Using variadic templates in this way is a possible solution:
#include <iostream> template<typename... Args> void func(double t, Args... args) ; template<typename... Args> void func(int t, Args... args) ; void func(int t) { std::cout << "int: "<< t << std::endl ; } void func(double t) { std::cout << "double: "<< t << std::endl ; } template<typename... Args> void func(double t, Args... args) { func(t) ; func(args...) ; } template<typename... Args> void func(int t, Args... args) { func(t) ; func(args...) ; } int main() { int x1 = 1, x2 = 5 ; double d1 = 2.5 , d2 = 3.5; func( x1 , d1, x2 ) ; func( x1 , d1, d2 ) ; }
This is not very elegant, but can help solve your problem. Another method would be to use two std::initializer_list , one for each type, for example:
#include <iostream> #include <initializer_list> void func2( std::initializer_list<int> listInt, std::initializer_list<double> listDouble ) { for( auto elem : listInt ) { std::cout << "Int: " << elem << std::endl ; } for( auto elem : listDouble ) { std::cout << "double: " << elem << std::endl ; } } int main() { func2( {10, 20, 30, 40 }, {2.5, 2.5 }) ; }
source share