Move Constructor Syntax

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Oct 11 18:07:48 UTC 2024


On Friday, October 11, 2024 9:01:49 AM MDT Manu via Digitalmars-d wrote:
> > What benefit do you see in treating copy constructors or move constructors
> > like they're normal constructors instead of explicitly treating them as
> > special - especially given that the compiler already has to treat them as
> > special in order to generate the correct code in many cases?
>
> Semantic uniformity. Special casing random things is D's main claim to fame
> and the source of almost everything wrong with the language.
> The overload selection rules should make the proper choice.

The overload resolution rules don't need to apply to move constructors,
because you never need to call them explicitly. That's what the move
function is for. And if move constructors have special syntax, then they're
clearly distinct from normal constructors, and it's clearer that they're
their own thing and not a normal constructor to be called or considered when
overloading.

In addition, if we type a move constructor as this(typeof(this)), then we
actually introduce more special cases thanks to the fact that the parameter
needs to semantically be ref but wouldn't be typed as ref (and typing it as
ref would make it conflict with copy constructors). Giving it separate
syntax avoids that problem, allowing us to mark it with ref without
conflicting with the copy constructor.

- Jonathan M Davis





More information about the Digitalmars-d mailing list