Static arrays, typeof and IFTI?
Jonathan M Davis
jmdavisProg at gmx.com
Tue Oct 11 15:10:54 PDT 2011
On Monday, October 10, 2011 22:55 Norbert Nemec wrote:
> 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])
It does if you pass it an actual static array. For instance,
std.bitmanip.bigEndianToNative has a similar declaration. The problem with
your example stems from the fact that the literal is a dynamic array, and the
compiler fails to take the implicit conversion into account when instantiating
the template. So, for instance
myfunc(cast(int[4])[1, 2, 3, 4]);
works. Obviously, that's not really what you're looking for, but the templated
function is just fine. It's just that template instantiation is very exact and
doesn't do much (if anything) in the way of conversions.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list