www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Count template parameters of method

reply Andrey <saasecondbox yandex.ru> writes:
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...
Oct 10 2020
parent reply Basile B. <b2.temp gmx.com> writes:
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.
Oct 11 2020
parent Andrey <saasecondbox yandex.ru> writes:
And what about:
 void test() {}
and
 void text(alias qqq)() {}
?
Oct 12 2020