Move Constructor Syntax

Paul Backus snarwin at gmail.com
Sun Oct 6 16:43:34 UTC 2024


On Sunday, 6 October 2024 at 04:04:28 UTC, Walter Bright wrote:
> As the above illustrates, a move constructor cannot be 
> distinguished from a regular constructor by syntax alone. It 
> needs semantic analysis.

It's worth pointing out that this is not just a problem for move 
constructors; it's also a problem for copy constructors. 
Specifically, it's what prevents copy constructors from being 
templates.

As a result, if you're writing generic code and want to handle 
arbitrary combinations of type qualifiers on the original object 
and the copy, you cannot do the simple, obvious thing and use a 
template, like this:

     this(this This, Other)(ref Other other)
     if (is(immutable Other == immutable typeof(this))
     {
         // copy logic
     }

Instead, you must write individual copy constructor overloads for 
every combination of qualifiers:

     // Function bodies omitted for brevity
     // mutable original
     this(ref typeof(this));
     this(ref typeof(this)) const;
     this(ref typeof(this)) immutable;
     this(ref typeof(this)) inout;

     // const original
     this(ref const typeof(this));
     this(ref const typeof(this)) const;
     this(ref const typeof(this)) immutable;
     this(ref const typeof(this)) inout;

     // immutable original
     this(ref immutable typeof(this));
     this(ref immutable typeof(this)) const;
     this(ref immutable typeof(this)) immutable;
     this(ref immutable typeof(this)) inout;

     // inout original
     this(ref inout typeof(this));
     this(ref inout typeof(this)) const;
     this(ref inout typeof(this)) immutable;
     this(ref inout typeof(this)) inout;

     // etc.

So, whatever solution we come up with for helping the compiler 
identify move constructors, I hope we can apply it to copy 
constructors too.


More information about the Digitalmars-d mailing list