static arrays becoming value types

dsimcha dsimcha at yahoo.com
Tue Oct 20 06:28:16 PDT 2009


== Quote from bearophile (bearophileHUGS at lycos.com)'s article
> Walter Bright:
> > The previous behavior for function parameters can be retained by making
> > it a ref parameter:
> >     void foo(ref T[3] a)
> If I have generic code, like a templated function, that accepts both a dynamic
and a static array, the function call will change its performance signature
according to the type (if I don't add a "ref" the pass of a dynamic array will be
O(1) while passing a fixed-size array will be O(n)).

Here's a way around that:  To pass a static array by reference to a templated
function that was written with generic ranges in mind, just slice it to make it a
dynamic array:

float[3] foo;
pragma(msg, typeof(foo[]).stringof);  // float[]



More information about the Digitalmars-d mailing list