How do I use `__rvalue` expression on a struct property?
Timon Gehr
timon.gehr at gmx.ch
Thu Apr 24 12:56:12 UTC 2025
On 4/24/25 10:23, Jiten Devlani wrote:
> 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.
Seems this is a bug, you can report it here:
https://github.com/dlang/dmd/issues/new
This workaround appears to work:
```d
this(Container rhs) @nogc @trusted nothrow {
scope tmp=&rhs.s;
this.s = __rvalue(*tmp);
}
```
More information about the Digitalmars-d
mailing list