Copy constructor hell

RazvanN razvan.nitu1305 at gmail.com
Wed May 8 13:19:24 UTC 2019


On Wednesday, 8 May 2019 at 12:47:10 UTC, 9il wrote:
> On Wednesday, 8 May 2019 at 11:19:02 UTC, 9il wrote:
>> On Wednesday, 8 May 2019 at 09:05:48 UTC, RazvanN wrote:
>>> [...]
>>
>> The copy constructor is autogenerated for S.
>>
>> Check: https://run.dlang.io/is/3sJTrf
>> -------
>> struct T
>> {
>>     int i;
>>     this(ref return scope inout typeof(this) src)
>>         inout @safe pure nothrow @nogc
>>     {
>>         i = src.i;
>>     }
>> }
>>
>> struct S
>> {
>>     T t;
>> }
>>
>> auto bar(T a)
>> {
>> 	S(a);
>> }
>> -------
>>
>> onlineapp.d(18): Error: copy constructor onlineapp.S.this(ref 
>> inout(S) p) inout is not callable using argument types (T)
>> onlineapp.d(18):        cannot pass argument a of type T to 
>> parameter ref inout(S) p
>
> It introduces regression for Phobos as well. For example,
>
> `DontCallDestructorT(arg)` is no longer possible for T arg.
>
> ---
> struct Nullable(T)
> {
>     private union DontCallDestructorT
>     {
>         T payload;
>     }
>  ....
> }

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.


More information about the Digitalmars-d mailing list