I managed to solve it using std::array
, inspired by the tuple solution in How do I expand a tuple into variadic template function's arguments?
(Edited : std::array version, first version used raw pointers)
// Function I want to call :
template< class... Args >
void f( Args&... args ) {
// ...
}
Using recursive templates who grab the arguments from the end of the array (so that they end up in the correct order).
A specialization at I = 0
has all the arguments in the Args..args
and calls f()
namespace Helper {
template< unsigned int N, unsigned int I >
struct Caller {
template< class T, class...Args >
static void call( const std::array<T,N>& arr, Args&... args ){
Caller<N,I-1>::call( arr, std::get<I-1>(arr), args... );
}
};
template < unsigned int N >
struct Caller<N, 0> {
template< class T, class...Args >
static void call( const std::array<T,N>& arr, Args&... args ) {
f(args...);
}
};
}
Let's wrap it up in a nice function
template< typename T, unsigned N >
void call_f( const std::array<T,N>& arr ){
Helper::Caller<N,N>::call(arr);
}
Here's what the calling code looks like.
std::array<float,3> array = {4.3, 3.14,2.1} ;
call_f(array);
Live version here