Error: functions cannot return static array float[4u][4u]

Steven Schveighoffer schveiguy at yahoo.com
Thu Feb 21 20:42:04 PST 2008


"Spacen Jasset" wrote
> For this member function:
>
> float[4][4] toFloatArray4x4()
> {
> float f[4][4];
> return f;
> }
>
> I get:
> Error: functions cannot return static array float[4u][4u]
>
> Is it the case that you can't return static arrays? Thinking about this it 
> may make some sense. Should I therefore return a float[][] (which will 
> always be a float [4]4]. I intend to assign it to a static float[4][4] in 
> most cases.
>
> Or rather, should I use an out/ref parameter like so:
>
> toFloatArray4x4(ref float f[4][4])
>
>
> I was also hoping to define a operator "overload" cast operator, but if I 
> can't 'return' static arrays then this will not work.
>
> I can return a struct from a function, why not a static array? I can sort 
> of see why this might not work since D objects and types are more dynamic 
> oriented than in C and C++ but it seems unnecessary to construct an object 
> on the heap ( a float[][] ) just to return it and assign it to a static 
> array.
>

This is really an unnecessary limitation as far as I can tell.  I think it's 
because static arrays are somehow treated as not fully-typed entities by the 
compiler.  It also hurts in lazy IFTI functions, i.e.:

writelazily(T)(bool dowrite, lazy T value)
{
    if(dowrite) writefln(value);
}

writelazily(true, "hello"); // does not compile

In your issue however, the following should work, and does not use the heap.

struct FloatArray4x4
{
   float f[4][4];
}

FloatArray4x4 toFloatArray4x4()
{...}

-Steve 




More information about the Digitalmars-d-learn mailing list