Move Constructors - Converting Lvalues to Rvalues

Manu turkeyman at gmail.com
Thu Oct 3 23:30:26 UTC 2024


On Fri, 4 Oct 2024 at 06:56, Timon Gehr via Digitalmars-d <
digitalmars-d at puremagic.com> wrote:

> On 10/3/24 17:47, Walter Bright wrote:
> >
> > __rvalue() is not a move, it just guides the overload resolution to the
> > move constructor/assignment. It's a hint.
>
> It has to be a move though. An rvalue is owned, an lvalue is not.
> Whenever an lvalue goes into an rvalue, it is either copy or move.
>
> If `__rvalue` is just some sort of ad-hoc type-punning operation
> exclusively for overload resolution, it will often copy the argument,
> unless your intention is to force it to be consumed by a function that
> is implicitly `@move` (such as a move constructor or a move assignment).
>

Yes, exactly... __rvalue() must be SOME KIND of move; it is explicitly
taking ownership away from the owner, and handing it to some new owner.
The thing about __rvalue() is that you will hide it inside some function,
like `T move(ref T t) => __rvalue(t)`
That encapsulates the ownership transfer, and separates the visibility of
the ownership transfer from the calling scope where that information is
needed.
Maybe you should reserve the name `move` and make it an error for a user to
produce any declaration with that name? That might avoid your surprise
overload issue?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20241004/f3495e0b/attachment-0001.htm>


More information about the Digitalmars-d mailing list