Copy constructor hell

9il ilyayaroshenko at gmail.com
Wed May 8 11:19:02 UTC 2019


On Wednesday, 8 May 2019 at 09:05:48 UTC, RazvanN wrote:
> On Wednesday, 8 May 2019 at 03:18:14 UTC, 9il wrote:
>> Hi,
>>
>> Just tried to upgrade mir-algorithm with the copy constructor.
>>
>> Mir has only a few types that should define copy constructors 
>> explicitly.
>>
>> However, Slice (main structure ) and more than 30 internal 
>> generic types like Iterators and Fields should be upgraded now 
>> as well. The compiler adds an automatically generated 
>> constructor. So, default structure construction that worked 
>> before, would not work anymore:
>>
>> struct S(T)
>> {
>>     T t;
>> }
>>
>> T oneT;
>>
>> S(oneT); // would not work anymore if T has a copy constructor.
>>
>> So, it is a big breaking change for user code as well.
>>
>> Can we fix it at least for types that have compiler generated 
>> copy-constructors?
>>
>> Best,
>> Ilya
>
> What is the signature of the copy constructor that you have 
> defined? Have you tried the this(inout typeof(this)) inout one?

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




More information about the Digitalmars-d mailing list