Move Constructor Syntax

Walter Bright newshound2 at digitalmars.com
Sun Oct 6 04:04:28 UTC 2024


```
struct S { ... }

this(ref S) // copy constructor
this(this)  // postblit
this(S)     // move constructor
~this()     // destructor

alias T = S;
this(T);    // also move constructor

alias Q = int;
this(Q);    // regular constructor
```
As the above illustrates, a move constructor cannot be distinguished from a 
regular constructor by syntax alone. It needs semantic analysis.

While this seems simple enough, it isn't I have discovered to my chagrin. The 
overload rules in the language (including things like rvalue references where 
sometimes an rvalue becomes an lvalue) that the move constructors get confused 
with the copy constructors, leading to recursive semantic loops and other problems.

I've struggled with this for days now.

A fix that would simplify the language and the compiler would be to have a 
unique syntax for a move constructor, instead of the ambiguous one in the 
proposal. That way, searching for a copy constructor will only yield copy 
constructors, and searching for a move constructor will only yield move 
constructors. There will be a sharp distinction between them, even in the source 
code. (I have seen code so dense with templates it is hard to figure out what 
kind of constructor it is.)

Something like one of:
```
1. =this(S)
2. this(=S)
3. <-this(S)
```
?

It may take a bit of getting used to. I kinda prefer (1) as it is sorta like 
`~this()`.


More information about the Digitalmars-d mailing list