I tried the following code:
#include <iostream>
struct test{
test(){}
test(test const &){
std::cout<<__LINE__<<"\n";
}
test(test&&){
std::cout<<__LINE__<<"\n";
}
};
#include <vector>
#include <utility> // std::move
int main(){
auto&& tmp = test();
std::vector<test> v;
v.push_back(tmp);
std::cout<<__LINE__<<"\n";
v.push_back(std::move(tmp));
return 0;
}
The vs2013 compiler output:
6 // copy
18
9 // move
9 // move
The g++ and clang++ output:
6 // copy
18
9 // move
6 // copy
My problems are:
Is the type of tmp test&&? Is tmp a rvalue?
If the type of tmp is test&&, why didn't the first push_back use move constructor?
Where did the last output come from? Why did vs2013 and g++ output different results?
Thanks.
Answer for 3rd problem: It comes from the reallocation as commented by andrew.punnett.