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

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon Apr 24 13:11:02 PDT 2017


On 4/24/17 2:48 PM, Stanislav Blinov wrote:
> On Monday, 24 April 2017 at 15:19:29 UTC, Manu wrote:
>
>> If you're going to pinch the guts of rvalue arguments, then this needs to
>> be 100% reliable.
>> This needs to be aggressively unit-tested, and probably documented that
>> this is the official pattern for rvalue construction/assignment
>> operations.
>
> Looking over your OP again, and the report made by Petar Kirov, I
> realized this is not quite right. We can't, generally speaking, treat
> this(const ref X x) as a copy ctor.
>
> Consider:
>
> struct X
> {
>     int* ptr;
> }
>
> struct Y
> {
>     int* ptr;
>
>     this(ref const X x)
>     {
>         // ptr == ???;
>         // typeof(x.ptr) == const(int*)
>         // seems like the only way to implement this is:
>         // ptr = new int(*x.ptr);
>     }
>
>     this(X x)
>     {
>         import std.algorithm.mutation : swap;
>         swap(ptr, x.ptr);
>     }
> }
>
> X x;
> Y y = x; // this(ref const X) is called
>
> Suddenly, we can't copy the pointer, or at least make a shallow copy of
> it, without violating const, and the latter is UB.

This is what inout is for. I'll update the bug report.

-Steve


More information about the Digitalmars-d mailing list