Move construction from !is(T == typeof(this))
Andrei Alexandrescu via Digitalmars-d
digitalmars-d at puremagic.com
Mon Apr 24 15:46:18 PDT 2017
On 04/24/2017 04:23 PM, ag0aep6g wrote:
> On 04/24/2017 08:48 PM, Stanislav Blinov wrote:
>> Speaking of const violation and UB, std.algorithm.mutation.move seems to
>> violate const without remorse:
>
> Yup. Even in @safe code, which is a bug.
> https://issues.dlang.org/show_bug.cgi?id=15315
Should fail in all situations, thanks for reporting.
The original code should work like this:
struct X {}
struct Y {
private X _x;
this()(auto ref X x)
{
static if (__traits(isRef, x))
{
_x = x;
}
else
{
import std.algorithm.mutation : move;
_x = move(x);
}
}
}
Note how I made the ctor templated so it accepts auto ref. The less
sophisticated version is simply:
struct X {}
struct Y {
private X _x;
this(ref X x)
{
_x = x;
}
this(X x)
{
import std.algorithm.mutation : move;
_x = move(x);
}
}
There should be ways to do this easier, i.e. add a forward() function to
std.algorithm.mutation. Contributions are welcome!
Andrei
More information about the Digitalmars-d
mailing list