Copy constructor hell

Paul Backus snarwin at gmail.com
Wed May 8 18:01:34 UTC 2019


On Wednesday, 8 May 2019 at 13:19:24 UTC, RazvanN wrote:
>
> The problems seems to be that the default constructor takes the 
> arguments by value so a copy needs to be performed, hence the 
> copy constructor use. This is pretty nasty. I will return with 
> the details after I sort this out.

Isn't the issue that the presence of a constructor disables the 
use of "struct literal" syntax [1] for construction? The compiler 
is trying to find a constructor that matches the given argument, 
but since the only constructor is the auto-generated copy 
constructor, it can't.

If you add an explicit constructor to S, the problem goes away:

struct HasCopyCtor
{
     this(ref HasCopyCtor other) {}
}

struct S
{
     HasCopyCtor member;
     // Comment out the line below and compilation fails
     this(HasCopyCtor arg) { member = arg; }
}

void main()
{
     S s = S(HasCopyCtor());
}

Runnable: https://run.dlang.io/is/F4tHky

[1] https://dlang.org/spec/struct.html#struct-literal


More information about the Digitalmars-d mailing list