I don't understand in the following example why the paramater in the assignement operator use the copy constructor and not the move constructor to be builded
struct Foo
{
int data;
Foo()
{
static int cpt = 1;
data = cpt++;
std::cout << "Foo\n";
}
Foo(const Foo& foo)
{
std::cout << "const Foo&\n";
data = foo.data;
}
Foo(Foo&& foo)
{
std::cout << "Foo&&\n";
data = foo.data;
foo.data = 0;
}
Foo& operator= (Foo foo) //<--- call the copy ctor and not the move ctor as I expected
{
data = foo.data;
std::cout << "operator=\n";
return *this;
}
Foo& operator+ (const Foo& foo)
{
data += foo.data;
return *this;
}
};
int main()
{
Foo f;
Foo f1;
Foo f3;
f3 = f + f1;
std::cout << f3.data;
std::cin.ignore();
return 1;
}
output:
Foo
Foo
Foo
const Foo&
operator=
3
compiler : MSVCS2012 CTP
EDIT:
"But if you say a = x + y, the move constructor will initialize that (because the expression x + y is an rvalue)..."
But in fact x + y is a rvalue only if operator +
is implemented in a "special" way so ?