Move Constructor Syntax

Salih Dincer salihdb at hotmail.com
Fri Oct 11 17:17:09 UTC 2024


On Friday, 11 October 2024 at 16:14:36 UTC, Manu wrote:
> ... I thought of an issue with separating the constructor into 
> a bespoke name; what happens when you do this:
>
> =this(S);
> this(S);
>
> Which is selected?
> What about:
>
> =this(S);
> this(T)(T); which overlaps the concrete case?
>
> This questions also exist in today's language with copy 
> constructors, but the rules are clear; regular overload 
> selection semantics.

For a move constructor to come into play, the object must first 
be created by the regular constructor. The move constructor 
facilitates transferring an already existing object to another 
location. In this sense, a move constructor can only be invoked 
if there is already an existing object.

Therefore, the concern about conflicts may be unfounded because 
the compiler first constructs the object, and if it needs to be 
moved, it invokes the move constructor.

Steps:

1. Regular constructor: The object is created.
2. Move constructor: If the object needs to be transferred to 
another place, the move constructor is called.

In the current D language with normal overload semantics, the 
selection rules are straightforward:

1. Move semantics is preferred when an rvalue (temporary object) 
is passed.

2. Copy semantics is preferred when an lvalue (an existing 
object) is passed.

Thus, there is no actual risk of conflict between the regular and 
move constructors, as moving can only occur after the object has 
been created.

SDB at 79


More information about the Digitalmars-d mailing list