Detecting array type without template specialization?
Jarrett Billingsley
kb3ctd2 at yahoo.com
Tue May 9 13:08:01 PDT 2006
Say I want a template to do different things if it's passed an array versus
a non-array type. I can do this easily with template specialization:
template Spoon(T)
{
void knife()
{
writefln("single");
}
}
template Spoon(T : T[])
{
void knife()
{
writefln("array");
}
}
...
mixin Spoon!(int) i;
mixin Spoon!(int[]) a;
i.knife();
a.knife();
That prints
single
array
As I'd like it to.
That works great, but what if I'm writing a much larger class, where most of
the functionality would be the same between the array and non-array
versions, and I only want to change, say, one function to work differently
for arrays? It would be redundant and cumbersome to have to copy most of
the contents of one template to the other.
Something like this can be done for classes versus non-classes:
template Spoon(T)
{
void knife()
{
static if(is(T : Object))
writefln("class");
else
writefln("non-class");
}
}
But is there any way to do this for array types?
More information about the Digitalmars-d-learn
mailing list