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


On Tue, Nov 30, 2010 at 2:53 PM, Roman Kononov <roman@binarylife.net> wrote:
> 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.

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.


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