Static Array Idiom not working anymore.

Steven Schveighoffer schveiguy at yahoo.com
Thu Jun 14 12:40:10 UTC 2018


On 6/14/18 7:07 AM, Guillaume Piolat wrote:
> On Tuesday, 12 June 2018 at 15:35:42 UTC, Steven Schveighoffer wrote:
>>
>> No, that's not what I mean. What I mean is:
>>
>> int[] arr = [1,2,3].s;
>> int[] arr2 = [4,5,6].s;
>>
>> Legally, the compiler is allowed to reuse the stack memory allocated 
>> for arr for arr2. The lifetime of the arr data is over.
>>
>> -Steve
> 
> https://github.com/p0nce/d-idioms/issues/150
> 
> Especially if the stdlib has a way to do this now.

The Phobos PR isn't merged yet, so I think it's still valid to have this 
idiom, but it needs to be changed such that the implicit slicing doesn't 
happen.

To be clear, I think the static array idiom is useful for many reasons 
(my favorite is avoiding issues with literal and type size mismatch), it 
just that the showcase usage leads to an instant dangling pointer and 
should be altered.

-Steve


More information about the Digitalmars-d-learn mailing list