c++ - c++11: Calling a variadic function with the elements of a vector -


there plenty of questions how call variadic function elements of tuple. e.g: how expand tuple variadic template function's arguments? problem bit different:

i have family of functions:

void f(int arg1); void f(int arg1, int arg2); ... 

i'd template:

template<size_t arity> void call(std::vector<int> args) {    ??? } 

that calls appropriate f args[0], args[1]...

here's working example:

#include <vector>  // indices machinery  template< std::size_t... ns > struct indices {     typedef indices< ns..., sizeof...( ns ) > next; };  template< std::size_t n > struct make_indices {     typedef typename make_indices< n - 1 >::type::next type; };  template<> struct make_indices< 0 > {     typedef indices<> type; };  void f(int) {} void f(int, int) {}  // helper function because need way // deduce indices pack  template<size_t... is> void call_helper(const std::vector<int>& args, indices<is...>) {     f( args[is]... ); // expand indices pack }  template<std::size_t arity> void call(const std::vector<int>& args) {     if (args.size() < arity) throw 42;     call_helper(args, typename make_indices<arity>::type()); }  int main() {     std::vector<int> v(2);     call<2>(v); } 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -