2010-11-30 20:40 CST, Jonathan Wakely <jwakely....@gmail.com> said: >On 30 November 2010 20:33, Roman Kononov wrote: >> $ cat test1.cc >> struct X { >> X()=default; >> X(X&&)=default; >> X(X const&)=delete; >> //some very large or non-copyable content >> }; >> >> X test() { >> X const x={}; >> { >> //a lot of code where I do not want to modify x [accidentally] >> } >> return x; >> } >> >> $ g++ -c -std=c++0x test1.cc >> test1.cc: In function 'X test()': >> test1.cc:13:10: error: use of deleted function 'X::X(const X&)' >> test1.cc:4:3: error: declared here > >How do you expect to return a non-copyable object by value?
If x is not const, the move constructor allows returning x. Gcc is even allowed to elide the movement and construct x directly into the return value. >However, that doesn't change the fact you're trying to move from a >const object, which is obviously wrong. Not really, because the 2 const objects are about to be destroyed.