I've been playing with Rvalue lately and am having a weird problem. Let's define some simple class called Foo that contains vector< int > :
class Foo { public: Foo(std::vector< int >&& v) : v_(v) {} private: std::vector< int > v_; };
A Foo instance can be created by passing the temporary element vector< int > as follows:
std::vector< int > temp; Foo(std::move(temp));
Now that I tried to execute this code, I noticed that the vector inside Foo built using copy-constructor instead of move-constructor. However, if I specify the constructor instead:
Foo(std::vector< int >&& v) : v_(std::move(v)) {}
Then, the move constructor of v_ must be called. Why is this so? Why is backup std::move(v) needed in the initialization list? Why can't the compiler infer the intent to call the vector movement mechanism, since the corresponding argument to the Foo constructor is given as an Rvalue reference?
By the way, I am using GCC 4.6 with the option -std = C ++ 0x.
Thank you for your help. PMJ
pmjobin
source share