static arrays at runtime with templates ?

Emil emilper at
Sun Feb 3 16:33:48 UTC 2019

I tried this on a whim and it appears to work:

auto static_array(T, size_t data_size)()
     T[data_size] data;
     return data;

void main() {
     import std.stdio: writeln;
     import std.traits: isStaticArray;

     auto data = static_array!(int, 5)(3);
     static assert(__traits(isStaticArray, data));

     auto data_longer = static_array!(int, 6)(2);
     static assert(__traits(isStaticArray, data_longer));

Is this for real, static arrays at runtime without manually 
allocating memory ? Is this legitimate or should I expect 
problems ?

More information about the Digitalmars-d-learn mailing list