Move Constructor Syntax

Timon Gehr timon.gehr at gmx.ch
Sat Oct 12 21:10:39 UTC 2024


On 10/11/24 09:25, Manu wrote:
> I don't see how elevating this declaration to a move-constructor 
> actually changes the semantics at all... I don't think it does? I think 
> this function ALREADY IS A MOVE CONSTRUCTOR (at least; it's an rvalue- 
> constructor, which is the same thing from a semantic perspective); it's 
> just working with an inefficient calling convention.
> Move semantics as proposed simply improve this code, no?

DIP1040 suggests to pass the argument implicitly by reference and to not 
call the destructor on it.

As I mentioned in my DConf talk, this can lead to accidental memory 
leaks. Particularly if it is existing code that previously assumed the 
destructor will run.

Another point where it will differ is automatic generation of move 
constructors and move `opAssign`. I think now it has to be done manually 
or you get copies.

Finally, for some reason, at the moment you cannot have both `this(ref 
S)` and `this(S)`. But `opAssign(ref S)` and `opAssign(S)` works. This 
moves around values in memory a bit more often though. A semantic move 
may just pass a reference further down the call stack. (IIRC non-POD 
struct types are always passed by `ref` in the ABI.)


More information about the Digitalmars-d mailing list