Discussion Thread: DIP 1040--Copying, Moving, and Forwarding--Community Review Round 1
Paul Backus
snarwin at gmail.com
Fri Mar 5 23:32:04 UTC 2021
On Friday, 5 March 2021 at 23:03:57 UTC, tsbockman wrote:
> On Friday, 5 March 2021 at 12:19:54 UTC, Mike Parker wrote:
>> This is the discussion thread for the first round of Community
>> Review of DIP 1040, "Copying, Moving, and Forwarding":
>
> From the DIP:
>> A Move Constructor for struct S is declared as:
>> this(S s) { ... }
>> A Move Assignment Operator for struct S is declared as:
>> void opAssign(S s) { ... }
>
> Is the parameter to these methods really pass-by-value?
>
> If so, why???
1) When you pass an rvalue to a by-value parameter in D, it's
moved, not copied.
2) If you have a by-ref overload and a by-value overload for the
same function, the ref one is called for lvalues and the by-value
one is called for rvalues.
In fact, this is already how you have to write your opAssign if
you want to support explicit move-assignment from rvalues of
non-copyable types. See for example SumType's opAssign overload
[1], and the corresponding unit test [2].
[1]
https://github.com/dlang/phobos/blob/51a70ee267026e150b9b5d81a66ad1fe33112623/std/sumtype.d#L629-L640
[2]
https://github.com/dlang/phobos/blob/51a70ee267026e150b9b5d81a66ad1fe33112623/std/sumtype.d#L1141-L1167
More information about the Digitalmars-d
mailing list