Do array literals still always allocate?

Namespace via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 14 00:53:18 PDT 2017


On Sunday, 14 May 2017 at 01:15:03 UTC, Lewis wrote:
> On Saturday, 13 May 2017 at 19:22:09 UTC, Steven Schveighoffer 
> wrote:
>> It's just out of date. Can't remember the version, but this 
>> did use to allocate. It doesn't any more. But only for this 
>> case. In most cases it does allocate.
>
> Okay cool, that's good to hear. For reference the most recent 
> place I remember seeing this was 
> http://stackoverflow.com/questions/6751575/how-to-initialise-static-arrays-in-d-without-a-gc-allocation (although I've definitely seen others in the past). I'll add an answer to the SO question to clarify that this is no longer an issue.
>
> Thanks all!

You could also use something like this:

----
auto s(T, size_t n)(T[n] arr)
{
     return arr;
}

auto is = [1, 2, 3].s; // stack allocated
----

I use it whenever I work with D.


More information about the Digitalmars-d-learn mailing list