Variable function templates for C ++ known type

I'm currently trying to figure out some things that I can do with support for variable templates. Say I have a function like this -

template <typename ... Args> void foo(Args ... a) { int len = sizeof...(tail); int vals[] = {a...}; /* Rest of function */ } /* Elsewhere */ foo(1, 2, 3, 4); 

This code works because I assume in advance that the arguments will be integers, but it will obviously work if I provide something else. If I know that parameter packages will contain a certain type in advance, is there a way I can do without templates and have something like -

 void foo(int ... a) 

I tried to do this, but the compiler gave an error stating that foo is a void field. I know that I can also access the parameters in the package through recursion, but I'm not sure that this will fix the problem that I have, namely, I want to be able to accept a variable number of arguments of the same type.

+6
source share
3 answers

This should work:

 void foo(int); template<typename ...Args> void foo(int first, Args... more) { foo(first); foo(std::forward(more)...); } 
+10
source

If you know the types earlier, you can use function overloading with std:initializer_list :

 #include <initializer_list> #include <iostream> void foo( std::initializer_list<int> l ) { for ( auto el : l ) // do something } void foo( std::initializer_list<float> l ) { } void foo( std::initializer_list<std::string> l ) { } int main() { foo( {1, 2, 3, 4 } ); foo( {1.1f, 2.1f, 3.1f, 4.1f } ); foo( { "foo", "bar", "foo", "foo" } ); return 0; } 

If you are using Visual Studio 2012, you may need the Visual C ++ Compiler November 2012 CTP .

EDIT: If you still want to use a variation pattern, you can:

 template <int ... Args> void foo( ) { int len = sizeof...(Args); int vals[] = {Args...}; // ... } // And foo<1, 2, 3, 4>(); 

But you must remember that it does not work with float and std::string , for example: you end up with 'float': illegal type for non-type template parameter . float not legal as a non-type template parameter , this is due to accuracy, floating point numbers cannot be represented exactly, and the likelihood that you are referring to the same type may depend on how the number is represented.

+3
source

I'm currently trying to figure out some things that I can do with support for variable templates.

Assuming that you want to experiment with variable templates, do not find any solution for your problem, then I suggest taking a look at the code below:

 #include <iostream> template<int ...Values> void foo2() { int len = sizeof...(Values); int vals[] = {Values...}; for (int i = 0; i < len; ++i) { std::cout << vals[i] << std::endl; } } int main() { foo2<1, 2, 3, 4>(); return 0; } 

The difference between foo2 and your foo is that you pass the parameters foo at runtime to foo2 at compile time, so for each set of parameters you use, the compiler will generate a separate foo2 body of the function.

+1
source

Source: https://habr.com/ru/post/950002/


All Articles