How to properly use variadic templates (functions)?

rempas rempas at tutanota.com
Tue Dec 21 09:28:59 UTC 2021


On Tuesday, 21 December 2021 at 08:42:35 UTC, vit wrote:
>
> You can use switch + static foreach:
>
> ```d
> import std.stdio;
>
>     //this print args in reverse order:
>     void print(T...)(string prompt, T args)
>     {
>         void print_arg(size_t index){
>             switch(index){
>                 static foreach(i, a; args){
>                 	case i:
>                         // handle your other types
>                         write(a);
>                     	return;	
>                 }
>                 default:
>                 	assert(0, "no impl");
>             }
>         }
>
>         write(prompt);
>         size_t len = args.length;
>         while(len --> 0)
>             print_arg(len);
>     }
>
>     void main(){
>         print("Prompt (ignored): ", "Hello", " world!\n", 123);
>     }
>
> ```

Cool! That's probably what I wanted to do! It seems that when 
looping inside a "static foreach" and taking the index, then I 
can compare it with a value that is not calculated at compile 
time. This way I can also check for the type of the variables and 
do my original plan which will make the function even better! 
Thanks a lot for the help dude!!!


More information about the Digitalmars-d-learn mailing list