[c++0x] cannot return a constant
Roman Kononov
roman@binarylife.net
Tue Nov 30 21:06:00 GMT 2010
2010-11-30 20:40 CST, Jonathan Wakely <jwakely.gcc@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.
More information about the Gcc
mailing list