I'm playing with prvalues, xvalues, etc... in this snippet.
#include<iostream> class A { public: A() : i(1) {} int i; }; A prvalue() { return A(); } A&& xvalue() { return std::move(A()); } int main() { A&& a1 = prvalue(); // Ok A&& a2 = xvalue(); // UB A&& a3 = std::move(A()); // Does it extend the lifetime of the temporary? A&& a4 = A(); // I believe this extends the lifetime of the temporary a1.i = 2; a2.i = 2; // UB a3.i = 2; // Is this UB? a4.i = 2; std::cout << a1.i << '\n'; std::cout << a2.i << '\n'; std::cout << a3.i << '\n'; std::cout << a4.i << '\n'; }
I can understand that A&& a2 = xvalue(); shows undefined behavior, because the temporary is destructed before the functionxvalue() returns. But what about the assignment A&& a3 = std::move(A());and A&& a4 = A();, do they extend the lifetime of the temporariesinvolved?
Apparently it's possible to call std::move() with a prvalue as argument. Is this correct?
See the output for the code here.
Edit:
There shouldn't be a problem with
A&& a4 = A();
as this is equivalent to the first assignment.