On 30 November 2010 20:40, Jonathan Wakely wrote:
> On 30 November 2010 20:33, Roman Kononov wrote:
>> This is related to http://gcc.gnu.org/ml/gcc/2010-11/msg00623.html
>>
>> I write about it again because the following seems too bad:
>>
>> $ 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?
>
> You could change it to return std::move(x) but you can't move from a const.

To fix this broken code, either
1) do not delete the copy constructor, you need it to return by value.
or
2) do not make 'x' const, and move from it with return std::move(x)


>> Another related example:
>>
>> $ cat test2.cc
>> struct U {
>>  U();
>>  U(U&&);
>>  U(U const&);
>> };
>>
>> struct X {
>>  U const u;
>>  X()=default;
>>  X(X&&)=default;
>>  //100 other members
>> };
>>
>> X test() {
>>  X a={};
>>  return a;
>> }
>>
>> $ g++ -c -std=c++0x test2.cc
>> test2.cc: In function 'X test()':
>> test2.cc:16:10: error: use of deleted function 'X::X(X&&)'
>> test2.cc:10:3: error: 'X::X(X&&)' is implicitly deleted because the default 
>> definition would be ill-formed:
>> test2.cc:10:3: error: non-static data member 'X::u' does not have a move 
>> constructor or trivial copy constructor
>
> This clearly tells you what's wrong.

To fix this broken code,
1) do not define the X move constructor as defaulted, because the
default definition attempts to move from U, which is const so the
default definition is ill-formed
and
2) define a copy constructor, explicitly-defaulted if you wish.

Reply via email to