I have a simple snippet:
class Object
{
private:
int value;
public:
Object(int value) : value(value) { cout << "Object::ctor\n"; }
Object(const Object& obj) { cout << "Object::copy-ctor\n"; }
Object(Object&& obj) { cout << "Object::move-ctor\n"; }
};
Object take_and_return_obj(Object o) { return o; }
int main()
{
Object o(5);
take_and_return_obj(o);
}
Now, this, as expected, prints a copy
and move
constructor.
Object::copy-ctor
Object::move-ctor
This is because o
gets copied into the function using the copy-ctor, and then gets sent back using the move-ctor since the function is over and the return value is an xvalue.
However, something happens when the initial argument to the function is also an xvalue:
int main()
{
Object o = take_and_return_obj(Object(5));
}
What happens is that somehow nothing happens when the value is sent to the function:
Object::ctor
Object::move-ctor
I assume that the move
is for the return operation, so that is not affected by this change. However there is no copy-ctor called to create the o inside the function's scope. I know its not any kind of pointer or reference since I made the function take the argument by value.
So my question is: what exactly happens to the xvalue I create in main
so that the argument inside the function gets its value?
This is more of an educational question, so do not be afraid to go into more in-depth answers.