Getting the overload set of a template

Simen Kjærås simen.kjaras at gmail.com
Thu Apr 19 17:55:47 UTC 2018


On Thursday, 19 April 2018 at 14:16:21 UTC, Alex wrote:
> On Thursday, 19 April 2018 at 13:57:04 UTC, Simen Kjærås wrote:
>> Currently, there is no way (that I've found, at least) to do 
>> this. If you have a workaround, that's great, but there really 
>> should be a way - probably __traits(getOverloads). Having 
>> __traits(getOverloads) return templates as well should fix 
>> some of the issues __traits(getOverloads) has, as a bonus.
>>
>
> Would it be possible at all? I mean, if the two following codes 
> are equivalent
> ´´´
>     @S("Has foo_A") template foo(string s) if (s == "a") {
>         enum foo = "foo_A";
>     }
>     @S("Has foo_B") template foo(string s) if (s == "b") {
>         enum foo = "foo_B";
>     }
> ´´´
>
>
> ´´´
>     template foo(string s)
>     {
>         static if (s == "a")
>         {
>                @S("Has foo_A") enum foo = "foo_A";
>         }
>         else static if (s == "b")
>         {
>               @S("Has foo_B") enum foo = "foo_B";
>         }
>     }
> ´´´
>
> How would you define a "template overload"?
> And which "overloads" would you like to get if constraints are 
> more general?
> And last but not least, the getOverloads is defined on 
> functions, which are callable, whereas templates are not, in 
> general...

Your first example defines two templates (which are overloads of 
the same name), the second only one. There's no ambiguity there.

Since template instantiation is analogous to function calling 
(though one is a compile-time action, the other a run-time one), 
talking about template overloads in the same way as function 
overloads makes perfect sense. Whether the result of 
instantiating the template is a function, a value, or a type, 
it's an overload of the template.

--
   Simen


More information about the Digitalmars-d-learn mailing list