new T[size] vs .reserve

monarch_dodra monarchdodra at gmail.com
Sat Feb 2 14:39:57 PST 2013


On Saturday, 2 February 2013 at 22:15:56 UTC, Namespace wrote:
> My real question was: will we ever have fixed size arrays at 
> runtime?

I see no reason why we couldn't, the only problem is a syntax 
one. If you beat the syntax (by wrapping it in a struct) then you 
can do it no problem:

//----
T[N]* makeFixed(size_t N, T)()
{
     static struct Fixed
     {
         T[N] a;
     }
     auto p = new Fixed();
     return &((*p).a);
}

void main()
{
    int[4]* p4 = makeFixed!(4, int)();
}
//----

And there, a dynamically allocated fixed size array. I know it's 
not pretty, but it proves the point.


More information about the Digitalmars-d-learn mailing list