Discussion Thread: DIP 1040--Copying, Moving, and Forwarding--Community Review Round 1
deadalnix
deadalnix at gmail.com
Thu Mar 18 19:53:49 UTC 2021
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.
D's pure attribute is the perfect example of this.
> [1]
> https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom
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.
More information about the Digitalmars-d
mailing list