On Tue, Nov 30, 2010 at 2:53 PM, Roman Kononov <ro...@binarylife.net> wrote: > 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.
exactly as expected. > Gcc is > even allowed to elide the movement and construct x directly into the > return value. copy elision does not waive verification of viable copy-constructor. > >>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. > yes, but that ir orthogonal to moving from a const object -- those are standard rules.