Move construction from !is(T == typeof(this))

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Thu Apr 27 05:28:38 PDT 2017


On 4/26/17 8:17 PM, Stanislav Blinov wrote:
> On Wednesday, 26 April 2017 at 13:38:59 UTC, Steven Schveighoffer wrote:

>> If you want to duplicate const data, but just shallow-copy mutable
>> data, you are correct in that you need two separate constructors, and
>> inout doesn't come into play.
>
> That's the thing, this is more about "need" than "want". As far as I
> understood, Manu is talking about these ctors from the C++ perspective,
> where the copy constructor takes const&.
> And the problem for those who come from C++ would be that in D it
> doesn't work that way, at least not universally.

My solution would be to push the duplication onto the user. I'm not a 
fan of implicit copying. It's also wasteful in the case of immutable data.

struct X
{
    char[] x;
    X dup() const { return X(x.dup); }
}

struct Y
{
    X x;
    inout this(inout(X) x) { this.x = x; }
}

X x;
const(X) cx;
auto y = Y(x);
auto cy = const(Y)(cx); // this could be simplified with factory
y = Y(cx.dup);

-Steve


More information about the Digitalmars-d mailing list