inferred size for static array initialization

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 2 12:08:52 PDT 2016


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


More information about the Digitalmars-d-learn mailing list