Move Constructors - Converting Lvalues to Rvalues
Richard (Rikki) Andrew Cattermole
richard at cattermole.co.nz
Fri Oct 4 07:58:08 UTC 2024
On 04/10/2024 8:15 PM, RazvanN wrote:
> On Thursday, 3 October 2024 at 15:35:36 UTC, Walter Bright wrote:
>> What if you have:
>
>> I'm also currently struggling with:
>> ```
>> this(S);
>> this(ref S);
>> ```
>> and then trying to get "is it copyable" to work. The trouble is that a
>> move constructor not only has an S for an rvalue, that S must also be
>> the same as the struct name. This cannot be determined by the parser,
>> it needs semantic analysis. This means that when the constructor is
>> searched for, it has to exclude matching on a move constructor, that
>> cannot be detected before semantic analysis.
>
> You can avoid this by simply moving the move and copy constructor to
> different overload sets during semantic analysis (__ctor, __cpctor,
> __mvctor).
> This will simplify the code immensely and it also has the added benefit
> that
> move and copy constructor will not be considered for manual constructor
> calls,
> which makes sense because mv and cp ctor are not meant for that.
In this vain, we could use an attribute to differentiate a copy
constructor and move constructor. Same signature apart from the attribute.
There is no reason why a move constructor couldn't do some cleanup of
the old value, to make it safe for a destructor call on it.
More information about the Digitalmars-d
mailing list