Eponymous template with full template syntax

monarch_dodra monarchdodra at gmail.com
Mon Jul 1 14:10:36 PDT 2013


On Monday, 1 July 2013 at 20:28:28 UTC, Ali Çehreli wrote:
> 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).

I though UFCS wasn't possible with constructors? *That* very 
usecase is one of the reasons why. Shouldn't that be an 
accepts-invalid?

> That is confusing.

UFCS construction: Yes. The rest, not so much:

The idea is that once a template is "eponymous", it *fully* 
becomes the eponymous function/type/value (s). Every other 
function, regardless of public/private*, simply seizes to exist 
to the outside world. You can't make a "qualified" call to an 
eponymous template, because the "qualification" is already the 
call. Long story short, it's not mix and match: Either you have a 
normal template, or you have an something eponymous, not a bit of 
both:

*What qualifies for eponymous template is kind of "buggy", since 
what actually qualifies is not exactly what the spec says. Still, 
*once* something is considered "qualified" by the implementation, 
then it is fully eponymous.


More information about the Digitalmars-d-learn mailing list