Eponymous template with full template syntax
Ali Çehreli
acehreli at yahoo.com
Mon Jul 1 13:28:27 PDT 2013
On 07/01/2013 12:03 PM, Maxim Fomin wrote:
> I think that this probably worked as early as in the end of 2011 but I
> can be wrong as don't remember exactly.
To answer Jonathan's question as well, it must have worked because I see
it in code that is definitely tested when it was written.
> It seems that dmd recognizes isSmall!int.isSmall as potential UFCS
> property, converts isSmall!int to bool and tries to issue call
> isSmall(bool) and fails, because that template does not define any
> function.
That explains it. :) Let's play with it a little:
import std.stdio;
template isSmall(T)
{
enum isSmall = (T.sizeof < 12345);
struct S
{
T m;
}
}
struct S
{
int[10] i;
}
void main()
{
writeln(isSmall!int);
writeln(isSmall!int.S.init);
writeln(isSmall!int.S);
}
First of all, apparently a template can include a definition with the
same name but I still cannot type isSmall!int.isSmall. I guess the above
is still an eponymous template and isSmall!int still means
isSmall!int.isSmall.
Now guess what the last two lines print. :) isSmall!int.S is *not* the S
that is included in the template! Here is the output:
true
S([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
S([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
The last line is actually an anonymous struct object of type S (the S
that is defined at module level). That is confusing.
Ali
More information about the Digitalmars-d-learn
mailing list