Count template parameters of method

Basile B. b2.temp at gmx.com
Sun Oct 11 10:10:04 UTC 2020


On Sunday, 11 October 2020 at 06:53:59 UTC, Andrey wrote:
> Hello,
>
> How to count a number of parameters in uninitialized template 
> method?
>
> For example:
>> struct Test
>> {
>>     void abc(int a, bool status, string text)() {}
>> {
>
> The method "Test.abc" has three template paramenters.
>
> I know that "TemplateArgsOf" exists but it is used only for 
> INITIALIZED templates...

You can count the commas in the text representation  but this is 
not a good solution. The default value of a 
TemplateValueParameter can lead to wrong results, for example if 
it's an array literal or a string literal.

---
size_t countTP(alias T)()
{
     import std.algorithm : count;
     return T.stringof.count(',') + 1;
}

template T1(A){}
template T2(A,B){}
template T3(A,B,C){}

void main()
{
     pragma(msg, countTP!T1);
     pragma(msg, countTP!T2);
     pragma(msg, countTP!T3);
}
---

probably the countTP function can be enhanced to handle corner 
cases I mentioned.


More information about the Digitalmars-d-learn mailing list