Why does this example print:
#include <iostream> struct X { X() = default; X(X const&) { std::cout << "copy-constructor\n"; } X(X&&) { std::cout << "move-constructor\n"; } X& operator=(X) { return *this; } }; int main() { X x, y; std::cout << "assign from prvalue calls the "; x = X{}; std::cout << "\nassign from xvalue calls the "; x = std::move(y); } assign from prvalue calls the
assign from xvalue calls the move-constructor
Both X{} and std::move(y) are rvalues so why does only assigning to X{} cause copy-elision?
X& operator=(const X&)andX& operator=(X&&), and you will see