Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two variadic templates for a single function?

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)
like image 757
Vincent Avatar asked Feb 23 '13 17:02

Vincent


People also ask

What is Variadic template C++?

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.

How do Variadic functions work in C?

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.

Is printf variadic function?

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.

Which of the following are valid reasons for using variadic templates in C++?

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.


1 Answers

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;
}
like image 180
Andy Prowl Avatar answered Oct 05 '22 17:10

Andy Prowl