Getting the overload set of a template

Alex sascha.orlov at gmail.com
Sun Apr 22 19:27:24 UTC 2018


On Sunday, 22 April 2018 at 18:25:29 UTC, Simen Kjærås wrote:
> 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.

So, how to distinguish it?

>
> 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.
>

The example you give, is not what I mean. Compare it with this:

@("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!"a"));
// tuple("Has foo1_A")
pragma(msg, __traits(getAttributes, foo2!"a"));
// tuple("Has foo1_B")
pragma(msg, __traits(getAttributes, foo1!"b"));
// tuple("Has foo1_B")
pragma(msg, __traits(getAttributes, foo2!"b"));


More information about the Digitalmars-d-learn mailing list