When I compile this with GCC:
struct A { A(); A(const A &); A(A &&) = delete; }; void ugh(A); A bar() { A a; ugh(a); return(a); }
I get an error message:
x.cpp: In function 'A bar()': x.cpp:14:13: error: use of deleted function 'A::A(A&&)' return(a); ^ x.cpp:5:5: note: declared here A(A &&) = delete; ^
In practical terms, it makes sense to consider a in the returned expression as an rvalue (since it must be destroyed). But does not mean that the standard should be anonymous?
In addition, even if in this case it is allowed to use the move constructor, why is it needed? Why can't the compiler use the copy constructor because the move constructor is not available?
The answer to his previous question, Does a local object return movement semantics? basically addresses mine, but does not affect how a named variable can become an rvalue without calling std :: move ().
EDIT: These two links explain why gcc behavior is correct, and sheds light on why the standard requires this behavior: Why are C ++ 11-remote functions involved in overload resolution? http://en.cppreference.com/w/cpp/language/return
source share