Yes, just use a base function taking a tuple then make a function of the same name with variadic templates/parameters.
template<typename ... Types>
void func(const std::tuple<Types...> &Tuple){
// work with tuple
}
template<typename TupleType, typename ... TupleTypes>
void func(const TupleType &arg, const TupleTypes &... args){
func(arg);
func(args...);
}
int main(){
func(std::make_tuple(1, 2), std::make_tuple("hello"));
}
This will give you a lot of flexibility in that you can know the number of types in the tuple
and throw
or static_assert
or whatever you like when you come across something you don't like OR you could go even further and use std::enable_if
on the function to only allow certain conditions with your tuples!
This function will only work with std::tuple
as you asked, but you could expand it further to allow the use of any container that takes multiple template arguments for it's types by changing the first function to:
template<typename ... Types, template<typename...> class TupleType>
void func(const TupleType<Types...> &Tuple){
// Work with any type inter-face-able like a tuple
}
EDIT
This can be made a little more terse with C++17 fold expressions:
template<typename ... Ts>
void func(const std::tuple<Types...> &Tuple){
// ...
}
template<typename ... Tuples>
void func(const Tuples &... tuples){
(func(tuples), ...);
}