How do I use `__rvalue` expression on a struct property?
Manu
turkeyman at gmail.com
Sun Apr 27 02:05:16 UTC 2025
FWIW; user code shouldn't contain __rvalue directly.
You can define a move function like:
ref T move(T)(ref T value) __rvalue => value;
Then you can do:
T x = var.move; // move construction
`forward` can be implemented similarly.
On Thu, 24 Apr 2025 at 18:26, Jiten Devlani via Digitalmars-d <
digitalmars-d at puremagic.com> 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.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20250427/2fbc6ed2/attachment.htm>
More information about the Digitalmars-d
mailing list