In C++11 is it possible to have two variadic templates for a single function ?
If not, is there a trick to write something like that :
template <class... Types, class... Args>
void f(const std::tuple<Types...>& t, Args&&... args)
A variadic template is a class or function template that supports an arbitrary number of arguments. This mechanism is especially useful to C++ library developers: You can apply it to both class templates and function templates, and thereby provide a wide range of type-safe and non-trivial functionality and flexibility.
Variadic functions are functions that can take a variable number of arguments. In C programming, a variadic function adds flexibility to the program. It takes one fixed argument and then any number of arguments can be passed.
The C printf() function is implemented as a variadic function. This noncompliant code example swaps its null-terminated byte string and integer parameters with respect to how they are specified in the format string.
Variadic templates are class or function templates, that can take any variable(zero or more) number of arguments. In C++, templates can have a fixed number of parameters only that have to be specified at the time of declaration. However, variadic templates help to overcome this issue.
That's perfectly legal:
#include <tuple>
using namespace std;
template <class... Types, class... Args>
void f(const std::tuple<Types...>& t, Args&&... args)
{
// Whatever...
}
int main()
{
std::tuple<int, double, bool> t(42, 3.14, false);
f(t, "hello", true, 42, 1.0);
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With