Allocating a fixed-size array on the heap
Jarrett Billingsley
kb3ctd2 at yahoo.com
Wed Jan 30 06:45:33 PST 2008
So, in the process of answering another thread, I realized that it doesn't
entirely seem possible to allocate a fixed-size array on the heap through
normal means. That is, the following is legal:
float[4] a;
float[4]* b = &a;
But:
float[4]* c = new float[4];
"Waiit," says the compiler, "the type of 'new float[4]' is float[]."
So here some syntactic sugar is getting in the way of writing what I mean!
I've come up with this:
T* alloc(T)()
{
struct S
{
T t;
}
return &(new S).t;
}
..
float[4]* d = alloc!(float[4]);
but that seems awfully hackish.
Am I missing something or is this just yet another instance of how
fixed-size arrays aren't treated as first-class citizens?
More information about the Digitalmars-d
mailing list