Move Constructor Syntax
Danni Coy
danni.coy at gmail.com
Thu Oct 10 01:03:42 UTC 2024
On Sun, Oct 6, 2024 at 3:06 PM Walter Bright via Digitalmars-d <
digitalmars-d at puremagic.com> wrote:
> ```
> 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()`.
>
>
as somebody who had to look up what a move constructor was.
So coming from a newb perspective.
My suggestion for a new syntax would be
*this(S)
as long as that doesn't screw up language analysis in other places, since
there is an association between linking to the original data in the move
constructor and pointers.
I think the existing syntax is cleaner though if you can make it work.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20241010/99a2442d/attachment.htm>
More information about the Digitalmars-d
mailing list