Discussion: Rvalue refs and a Move construtor for D
Suleyman
sahmi.soulaimane at gmail.com
Fri Sep 6 21:13:12 UTC 2019
On Friday, 6 September 2019 at 19:50:30 UTC, kinke wrote:
> On Friday, 6 September 2019 at 19:25:57 UTC, Manu wrote:
>> There's another case where you can attribute a method with
>> `@rvalue` to apply to the `this` reference. This should
>> naturally work in D under the `@rvalue` proposal.
>>
>> Ie:
>>
>> struct S
>> {
>> Thing member;
>> ref Thing fun() { return member; }
>> Thing fun() @rvalue { return move(member); }
>> }
>>
>> This can be very valuable, and even more so in D where we use
>> a lot of UFCS chains.
>
> Oh wow, this might be able to convince me, as returning an
> rvalue ref from a method invoked on an rvalue instance would
> allow absolutely perfect forwarding of members (no copy/move
> and preserving rvalue-ness):
>
> struct S
> {
> Thing member;
> ref Thing fun() { return member; }
> @rvalue ref Thing fun() @rvalue { return member; }
> // maybe we could even represent both at once via something
> like:
> // auto ref Thing fun()() { return member; }
> }
I'm sorry but this looks like a bad idea. Moving selective parts
of S simply because it's all marked for move is bad. What happens
when the member is moved separately thus leaving an "empty" spot
inside S then S itself is moved. That would result is a half
valid object.
More information about the Digitalmars-d
mailing list