I looked it up and function returns are prvalues. Or at least in this case,
Code:
auto my_unique_ptr = get_unique_ptr();
,
get_unique_ptr() is a prvalue. And then this leads to the resolution of:
Code:
unique_ptr& operator=( unique_ptr&& r );
Edit:
Okay, I tested this in cpp.sh.
I couldn't not get RVO to work for construction/assignment :P
But this does show that the function return does indeed invoke move semantics:
Code:
// Example program
#include <iostream>
struct moveable
{
moveable(void)
{
std::cout << "invoking default constructor\n";
}
moveable(moveable const& rhs) = delete;
moveable(moveable&& rhs)
{
std::cout << "invoking move constructor\n";
}
auto operator=(moveable&& rhs) -> moveable&
{
std::cout << "invoking move assignment operator\n";
return *this;
}
};
auto get_moveable(void) -> moveable
{
moveable m;
return m;
}
int main(void)
{
moveable m;
m = get_moveable();
return 0;
}
/*
prints:
invoking default constructor
invoking default constructor
invoking move assignment operator
*/
I'm sure xvalues might do the same? I'm not sure how to manually trigger and create an xvalue.