Initialization of std.typecons.RefCounted objects

Matthias Walter xammy at xammy.info
Wed Jul 18 23:48:42 PDT 2012


On 07/18/2012 03:32 PM, Christophe Travert wrote:
> Matthias Walter , dans le message (digitalmars.D:172673), a écrit :
>> I looked at Bug #6153 (Array!(Array!int) failure) and found that the
>>
>> This exactly is what makes the following code fail:
>>
>> Array!(Array!int) array2d;
>> array2d.length = 1;
>> array2d[0].insert(1);
>>
>> The inner array "array2d[0]" was not initialized and hence the reference
>> pointer is null. Since Array.opIndex returns by value, the 'insert'
>> method is called on a temporary object and does not affect the inner
>> array (still being empty) which is stored in the outer array.
>>
>> What do you think about this?
>>
>> Must the user ensure that the Array container is always initialized
>> explicitly? If yes, how shall this happen since the only constructor
>> takes a (non-empty) tuple of new elements. Or shall opIndex return by
>> reference?
> 
> I think opIndex should return by reference. opIndexAssign is of no help 
> when the user want to use a function that takes a reference (here 
> Array.insert). It is normal that Array uses default construction when 
> someone increases the array's length.

Okay, I fully agree here.

> Besides that point, I don't see why default-constructed Array have an 
> uninitialised Payload. This makes uninitialised Array behaves 
> unexpectedly, because making a copy and using the copy will not affect 
> the original, which is not the intended reference value behavior.

Well the reason is that no user-defined function is called when a struct
is default-constructed and then copied into another variable (the first
one is this(this) which only known the new variable). Hence no user code
can be written that would initialize the payload. Do we need a paradigm
to perform explicit initialization of structs?

Best regards,

Matthias


More information about the Digitalmars-d mailing list