inferred size for static array initialization

Namespace via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 2 12:12:30 PDT 2016


On Monday, 2 May 2016 at 19:08:52 UTC, Steven Schveighoffer wrote:
> On 5/2/16 3:02 PM, Namespace wrote:
>> On Monday, 2 May 2016 at 18:57:49 UTC, Namespace wrote:
>>>> A slice of a no-longer-existing temporary! Admittedly, this 
>>>> is not an
>>>> issue with your code, but a deeper issue of allowing slicing 
>>>> of rvalues.
>>> This works:
>>> ----
>>> int[] as = [1, 2, 3].s;
>>> writeln(as[2]);
>>
>> Of course this slice is only valid as long as you dont leave 
>> the scope.
>> That's maybe what you tried to say, right?
>
> No, because 'as' is pointing at stack space no longer 
> allocated. It may work, and it may not, but having it work is 
> not proof that it's sound.
>
> To make things clear, this is what your code effectively does:
>
> int[] as = void;
> {
>    auto _tmp = [1, 2, 3].s;
>    as = _tmp;
> }
>
> Which is not the same thing as having the static array a 
> defined stack variable in the same scope.
>
> -Steve

The assembler looks different than that but I may be wrong and I 
only looked at gdc.
But anyway, that means with 'pragma(inline, true)' I'm save. Is 
that right?


More information about the Digitalmars-d-learn mailing list