Determining if a class has a template function

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Oct 14 05:29:27 PDT 2016


On Friday, 14 October 2016 at 09:15:40 UTC, Marc Schütz wrote:
> On Wednesday, 12 October 2016 at 16:57:50 UTC, Meta wrote:
>> There's also a *very* ugly hack you can do:
>>
>> //A template function's .stringof is of the format <function 
>> name>(<template args>)(<function args>)
>> //so match on the number of brackets to determine whether it's 
>> a template function or not
>> enum isTemplateFunction = __traits(isTemplate, f)
>>                               && fstr.balancedParens('(', ')')
>>                               && (fstr.canFind("if")
>>                                   || fstr.count!(c =>
>>                                          
>> cast(bool)c.among!('(', ')')) == 4);
>
> This won't work if there are additional parens _inside_ 
> compile/runtime parameters, though, which there is ample 
> opportunity for:
>
> T func(T : U!(int, string), U)(const(T) param = 
> defaultValue!(const(T))());

You're right... And this is why it's such an ugly hack. It can 
probably be improved upon but I would not recommend trying, 
although there's no other way I can think of to determine whether 
a template is a function template.


More information about the Digitalmars-d-learn mailing list