Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

stl

stddeque

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?

like image 617
user3161924 Avatar asked Jun 24 '26 13:06

user3161924


1 Answers

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.

like image 149
Eugene Avatar answered Jun 27 '26 02:06

Eugene