最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - std:move a std::string from a std::deque then std::deque::pop_front? - Stack Overflow

programmeradmin2浏览0评论

While I usually use my own classes created before STL existed. I decided to use these STL objects and function in a standalone library that was already using STL. I have a question about taking over a pointer (similar to MFC having a .Detach() method/function).

I have a worker thread with a queue of std::strings (in std:deque) where I want to, while guarded, assign the next item (front) of the queue to a local std::string variable then .pop_front the item out of the queue and releasing the guard (scoped operation).

I don't want to waste time and resources having it make a copy when all it needs to do is assign the pointer.

So my question is, does the STL handle this so that the destruction of the two std::string objects (referencing same pointer) will be okay (internal reference counting or nullifying the internals of the moved source)?

So basically:

void fun()
{
  std::string my_string;
  {
    std::lock_guard<std::mutex> guard(my_mutex);
    my_string = std::move(deque.front()); 
    deque.pop_front()
  }
  // ... go on and using my_string below
}

TIA!!

发布评论

评论列表(0)

  1. 暂无评论