2010-11-30 20:46 CST, Jonathan Wakely <[email protected]> said:
>On 30 November 2010 20:40, Jonathan Wakely 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;
>>> }
>
>To fix this broken code, either
>1) do not delete the copy constructor, you need it to return by value.
Cannot do it because of "//some very large or non-copyable content".
>or
>2) do not make 'x' const, and move from it with return std::move(x)
With x being non-const, move() is not required. And I really like using
"const" for a number of reasons. This is the crux of the question.
>>> $ 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;
>>> }
>
>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
Without the defaulted constructor I would have to type the code moving
all "//100 other members".
>2) define a copy constructor, explicitly-defaulted if you wish.
What if the copy constructor is too expensive and I have to use move
constructor?
Thanks