Static arrays, typeof and IFTI?

Norbert Nemec Norbert at Nemec-online.de
Mon Oct 10 22:55:02 PDT 2011


On 10.10.2011 08:58, Gor Gyolchanyan wrote:
> There is a huge difference between a static and a dynamic array.
> Dynamic array is an indirected type (objects of that type are pointers
> to the actual data), while static arrays are PODs (directly refers to
> the data).
> Dynamic arrays are always size_t.sizeof * 2 bytes long, while static
> arrays are typeof(T[0]).sizeof * T.length bytes long. That makes
> static arrays expensive to move around. Making the array literals
> static by default would mean unnecessary copying most of the time. In
> those few cases, where it is necessary, you can explicitly make them
> static arrays.

Actually, my problem is not so much the choice of the "default" but that
there is no easy way to work around it:

I couldn't find any way to force a variable or function argument to be
of static array type without explicitly stating the length. In an
assignment like
	int[3] myvar = [1,2,3,4]
the compiler will complain. However there does not seem to be any way to
state
	int[...whatever...] myvar = [1,2,3,4];
	enum N = typeof(myvar).init.length;
and let the compiler deduce that N==4.

Even worse for template arguments. My first idea (inspired by C++, also
suggested by kennytm)

	template(T,int N) myfunc(T[N] arg)

does not work in D and I could not find any alternative way to allow writing
	
	myfunc([1,2,3,4])

and determine the length of the static array at compile time.



More information about the Digitalmars-d mailing list