Getting the overload set of a template

Simen Kjærås simen.kjaras at gmail.com
Sun Apr 22 18:25:29 UTC 2018


On Saturday, 21 April 2018 at 21:10:29 UTC, Alex wrote:
> On Saturday, 21 April 2018 at 19:51:05 UTC, Simen Kjærås wrote:
>> On Saturday, 21 April 2018 at 11:23:33 UTC, Alex wrote:
>>> So, do you mean, that the constraint belongs to the interface 
>>> of a template?
>>
>> Not necessarily - it depends on what you want to achieve. The 
>> only thing I mean is that the code clearly defines two 
>> templates in one case (and chooses which template to 
>> instantiate based on the arguments), and one template in the 
>> other (and chooses the contents of that template based on the 
>> arguments).
>
> This is wrong, at least as I understand this:
> Lowering rules apply. These rules define a set of semantic 
> equivalent constructs. I expect all semantic equivalent 
> constructs to be handled equivalent.**

No lowering occurs here. A lowering is when the compiler takes 
one piece of syntax and replaces it with a different one, usually 
one that's more verbose.

In a way, it's kind of like a template being instantiated, in 
that you write some code and it's being replaced by something 
else. It's not, however, the same thing.

Templates are sort of like compile-time functions - overloads are 
chosen in a similar way way to how function overloads are chosen, 
and the result may depend on the parameters given. Two templates, 
just like two functions, can give the same result without the 
templates themselves being the same.


>> *In this case, there are important differences - in the first 
>> case the template itself is marked with a UDA, in the second 
>> the enum member is.
> This is a good point. But you won't be able to get the UDA from 
> an uninstantiated template will you? If you will, then, I'm 
> wrong, I think...

Why not try it?

@("Has foo1_A") template foo1(string s) if (s == "a") {
     enum foo1 = "foo1_A";
}
@("Has foo1_B") template foo1(string s) if (s == "b") {
     enum foo1 = "foo1_B";
}

template foo2(string s)
{
     static if (s == "a")
     {
            @("Has foo2_A") enum foo2 = "foo2_A";
     }
     else static if (s == "b")
     {
           @("Has foo2_B") enum foo2 = "foo2_B";
     }
}

// tuple("Has foo1_A")
pragma(msg, __traits(getAttributes, foo1));
// tuple()
pragma(msg, __traits(getAttributes, foo2));

So yes, we can. Templates are real things and can be manipulated, 
passed to other templates, etc.

--
   Simen


More information about the Digitalmars-d-learn mailing list