Variadic Template
In C++ templates may use to declare to get several version
1 |
template<typename ... Param> |
In here you may find a way to declare a type called Param but not only one strict type.
You may use it with other type names to
1 2 |
template<typename T, typename ... Param> void Display(T a, Param ... args){} |
And of course, you may create a function that calls himself recursively. But it needs its empty version too.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> void Display(){ std::cout << std::endl; } template<typename T, typename ... Param> void Display(T && a, Param && ... args){ std::cout << a << ":" << sizeof... (args) << "\t"; Display(std::forward<Param>(args) ...); } int main() { Display(9,2,1,3,1,2,1.1,"c"); } |
forward method may be used for possible move constructors.
Sizeof… is a standard method to get the count of the nested arguments.
1 Response
[…] The variadic template uses stf:forward […]