Suppose I have a template, for which I provide a default parameter type. Is
there any way of ensuring that this default will be respected unless the user
explicitly requests an alternative type?
As an example, consider the following:
//////////////////////////////////////////
import std.stdio;
void printSize(T = real)(T x)
{
writeln(x.sizeof);
}
void main()
{
float x;
x.printSize();
x.printSize!float();
x.printSize!real();
}
//////////////////////////////////////////
Here, a default type for T is given -- real -- but passing it a float overrides
that default, which you can see because the printout of x.sizeof gives the size
of a float instead of a real. In other words it outputs
4
4
16
Instead, what I'd like is a way to specify the template parameters so that the
type would be _real unless otherwise specified_ -- in other words, so that the
above would output
16
4
16
Reading through http://dlang.org/templates-revisited.html I can't see an evident
way to do this. I've tried T:real in place of T = real, but the same behaviour
results and in any case I don't think that syntax is intended for this kind of
purpose. (Actually, the given description of T:int as indicating "T must be int
type" would suggest that T:real would force T to be a real in all circumstances,
but that's not what happens ...)