Search code examples
c++stlstddeque

Will adding std::string items to std::deque with std::move be more efficient?


In this code:

// build string requiring a bunch of processing
std::wstring xmlstr=xml->GetXml();

{
  std::lock_guard<std::mutex> guard(my_mutex);
  m_deque.push_back(std::move(xmlstr));  // << note the std::move
}

Is it more efficient to use std::move in this case to reduce making copies?


Solution

  • Is it more efficient to use std::move in this case to reduce making copies?

    Yes, it is. push_back has an overload taking rvalue reference which will avoid copying. This is true for any container having push_back, including std::deque.