Template Usage with Eponymous Trick

Steven Schveighoffer schveiguy at gmail.com
Sun Feb 2 18:30:17 UTC 2020


On 2/2/20 12:51 PM, ShadoLight wrote:

> // Should this assertion pass or fail?
> static assert(is(a));  //PASS
> static assert(is(b));  //FAIL
> 
> But I don't see how braces will affect this. Can you explain?

First, note that:

struct S(T) {}

is *exactly* equivalent to (in fact, you can write it this way, and it 
works exactly the same):

template S(T) { struct S {} }

So does S!int refer to the template instantiation (which is not a type) 
or the eponymous S struct inside the template (which is a type)? This 
was Paul's point. Since you didn't use any members, there is an 
ambiguity of intention (if accessing other parts of the template are 
allowed).

Even if you use a member, it's not hard to come up with an ambiguous 
example:

struct S(T) {
   template S(X) {}
}

What does S!int.S refer to? The S template inside the S struct, or the S 
struct itself inside the outer S template?

This is the main reason why eponymous templates were disallowed from 
using the classical template access syntax. I think this was not a 
mistake, and we are better off for it.

-Steve


More information about the Digitalmars-d-learn mailing list