Discussion Thread: DIP 1040--Copying, Moving, and Forwarding--Community Review Round 1

Q. Schroll qs.il.paperinik at gmail.com
Fri Mar 19 00:08:39 UTC 2021


On Thursday, 18 March 2021 at 19:53:49 UTC, deadalnix wrote:
> On Wednesday, 17 March 2021 at 17:14:04 UTC, Q. Schroll wrote:
>> It might be a stupid question, but why have move assignment in 
>> the first place? In C++, there's the copy-and-swap idiom[1]. 
>> Maybe it's obvious why it does not apply in D, but if using a 
>> swap function makes implementing a copy assignment and move 
>> assignment trivial, why not requiring opSwap instead of 
>> opAssign for an elaborate move object?
>>
>
> This isn't a stupid question, this is THE question. It is easy 
> to assume things are necessary because other went there and did 
> it, but I find that questioning these assumptions is how the 
> greatest design ideas came up.

My suggestion isn't really don't do ever implement opAssign (the 
copy one and the move one) manually, but rather: Leave it up to 
the compiler, except you know you really cannot use copy and swap.

> Doing this has major issue: it require all movable structs to 
> have a null state (as in C++) or make other unsavory tradeofs 
> (see 
> https://forum.dlang.org/post/bkfqchwpnonngjrtybbe@forum.dlang.org for a more thorough explanation).
>
> Nevertheless, if the struct naturally has a null state, this is 
> indeed a very good way to do it.

I was never sure if that was a good or bad decision D made, but 
doesn't D require every type to have a null value (.init)? Or do 
you mean something else?



More information about the Digitalmars-d mailing list