How do I use `__rvalue` expression on a struct property?
Jiten Devlani
jdevlani at yahoo.in
Thu Apr 24 08:23:18 UTC 2025
I'm exploring the new `__rvalue` expressions, and got stuck while
creating a move constructor for a container(non-copyable) with a
non-copyable container property.
```d
import core.stdc.stdio : printf;
import core.memory : pureMalloc, pureFree;
struct S {
@("mallocd") ubyte[] p;
this() @disable;
this(this) @disable;
this(ubyte[] p) @nogc @trusted nothrow {
printf("Base constructor!\n");
this.p = p;
}
this(S rhs) @nogc @trusted nothrow {
printf("Move constructor!\n");
p = rhs.p;
rhs.p = null;
}
~this() pure @nogc @trusted nothrow {
if (p) {
pureFree(p.ptr);
p = null;
}
}
}
struct Container {
S s;
this() @disable;
this(this) @disable;
this(S s) @nogc @trusted nothrow {
this.s = __rvalue(s);
}
this(Container rhs) @nogc @trusted nothrow {
this.s = __rvalue(rhs.s);
}
}
```
Above code results in compilation error:
```
<source>(35): Error: struct `example.S` is not copyable because
it has a disabled postblit
this.s = __rvalue(rhs.s);
```
I've looked into the language reference but could not find a way
to move a property of a rvalue object instance.
More information about the Digitalmars-d
mailing list