I was playing with this example in order to understand rvalue references:
#include <string>
#include <iostream>
#include <utility>
#include <vector>
class Dog
{
public:
Dog() {};
Dog(Dog&& a) { std::cout << "R value" << std::endl;}
};
Dog foo()
{
return Dog();
}
int main()
{
std::vector<Dog> v;
v.push_back(Dog()); // calls move constructor
Dog c((Dog())); // does not call move constructor
Dog d(foo()); // does not call move constructor
}
I struggle to understand why in the line v.push_back(Dog()), object Dog() is treated as an Rvalue (so the move constructor is called), but the following two lines do not call move constructors. I guess I might be misunderstanding the relationship between an anonymous object and RValue here.