This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [c++0x] cannot return a constant


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]