Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

avoid copys when pushing an element into std::queue

Tags:

I am new to c++11 and would like to have a std::queue storing instances of class X and try to avoid unnecessary copies in push operation. In c++11, I found push() has a rvalue reference version:

void push (value_type&& val);

So does the following implementation avoids unnecessary copy of X

std::queue<X> my_queue;

for (...) { // some for loop
  X x;
  ... // some initialization of x
  my_queue.push(std::move(x));
}

compared to the following naive implementation?

std::queue<X> my_queue;

for (...) { // some for loop
  X x;
  ... // some initialization of x
  my_queue.push(x);
}
like image 311
keelar Avatar asked Feb 12 '14 23:02

keelar


1 Answers

The best way to answer to such question yourself is to create a probe object.

#include <iostream>
struct probe {
    probe() { std::cout << "probe()" << ((void*)this) << std::endl; }
    probe(const probe&) { std::cout << "probe(c&)" << ((void*)this) << std::endl; }
    probe(probe&&) { std::cout << "probe(&&)" << ((void*)this) << std::endl; }
    ~probe() { std::cout << "~probe()" << ((void*)this) << std::endl; }
};

and use it instead of X in the test.

I have created a playground here.

Whether or not the extra copy of anything is performed when you do std::move solely depends on what you type in the rvalue reference constructor.

like image 95
bobah Avatar answered Sep 23 '22 10:09

bobah