I am confused about using std::move()
in below code:
If I uncomment line at (2) the output would be: 1 2 3
but if I uncomment line at (1) output would be nothing which means that move constructor of std::vector
was called!
Why do we have to make another call to std::move
at (1) to make move constructor of std::vector
to be called?
What I understood that std::move
get the r-value
of its parameter so, why we have to get the r-value
of r-value
at (1)?
I think this line _v = rv;
at (2) is more logical and should make std::vector
move constructor to be called without std::move
because rv
itself is r-value
reference in the first place.
template <class T>
class A
{
public:
void set(std::vector<T> & lv)
{
}
void set(std::vector<T> && rv)
{
//_v = std::move(rv); (1)
//_v = rv; (2)
}
private:
std::vector<T> _v;
};
int main()
{
std::vector<int> vec{1,2,3};
A<int> a;
a.set(std::move(vec));
for(auto &item : vec)
cout << item << " ";
cout << endl;
return 0;
}