February 22, 2008 Re: Error: functions cannot return static array float[4u][4u] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Spacen Jasset | On Fri, 22 Feb 2008 00:25:44 +0000, Spacen Jasset wrote: > Derek Parnell wrote: >> why can't D handle 'ref' and 'out' static arrays either? >> In other words, this below won't compile... >> >> void toFloatArray4x4(ref float[4][4] p) ... >> >> So what is the rationale for that? >> > ... arrays have pass by reference semantics unlike stucts, ints and other basic types. Agreed, but why is that a problem? The way I see it, the called function gets the address of the first element in the static array and the compiler knows the layout of the array "[4][4]' so why can't the called function use it like that? Currently, it seems like an artificial restraint. I can't see why this below should be hard to implement... // ------------- void toFloatArray4x4(ref float[4][4] p) { int n; n = 1; for (int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) { p[i][j] = n; n++; } } } void main() { float[4][4] r; toFloatArray4x4( r ); } // --------- -- Derek (skype: derek.j.parnell) Melbourne, Australia 22/02/2008 1:03:40 PM |
February 22, 2008 Re: Error: functions cannot return static array float[4u][4u] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Spacen Jasset | "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
|
February 25, 2008 Re: Error: functions cannot return static array float[4u][4u] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Spacen Jasset | The only way I've found to do this sort of thing without wrapping is via a pointer: /* matrix is a static, or class member */ float[4][4] * toFloatArray4x4() { return &matrix; } Used: float v[4][4] = *m1.toFloatArray4x4(); I tried also concocting a system whereby I would return a float[][] object, but it doesn't seem easy to prepare such an object from a static array. I feel that multi-dimensional array workings are a bit unintuitive. |
February 26, 2008 Re: Error: functions cannot return static array float[4u][4u] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Spacen Jasset | Does Walter Bright recommend a way to return static arrays? |
Copyright © 1999-2021 by the D Language Foundation