Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store variadic template arguments?

Is it possible to store a parameter pack somehow for a later use?

template <typename... T> class Action { private:             std::function<void(T...)> f;     T... args;  // <--- something like this public:     Action(std::function<void(T...)> f, T... args) : f(f), args(args) {}     void act(){         f(args);  // <--- such that this will be possible     } } 

Then later on:

void main(){     Action<int,int> add([](int x, int y){std::cout << (x+y);}, 3, 4);      //...      add.act(); } 
like image 672
Eric B Avatar asked Jun 01 '13 01:06

Eric B


2 Answers

To accomplish what you want done here, you'll have to store your template arguments in a tuple:

std::tuple<Ts...> args; 

Furthermore, you'll have to change up your constructor a bit. In particular, initializing args with an std::make_tuple and also allowing universal references in your parameter list:

template <typename F, typename... Args> Action(F&& func, Args&&... args)     : f(std::forward<F>(func)),       args(std::forward<Args>(args)...) {} 

Moreover, you would have to set up a sequence generator much like this:

namespace helper {     template <int... Is>     struct index {};      template <int N, int... Is>     struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};      template <int... Is>     struct gen_seq<0, Is...> : index<Is...> {}; } 

And you can implement your method in terms of one taking such a generator:

template <typename... Args, int... Is> void func(std::tuple<Args...>& tup, helper::index<Is...>) {     f(std::get<Is>(tup)...); }  template <typename... Args> void func(std::tuple<Args...>& tup) {     func(tup, helper::gen_seq<sizeof...(Args)>{}); }  void act() {    func(args); } 

And that it! So now your class should look like this:

template <typename... Ts> class Action { private:     std::function<void (Ts...)> f;     std::tuple<Ts...> args; public:     template <typename F, typename... Args>     Action(F&& func, Args&&... args)         : f(std::forward<F>(func)),           args(std::forward<Args>(args)...)     {}      template <typename... Args, int... Is>     void func(std::tuple<Args...>& tup, helper::index<Is...>)     {         f(std::get<Is>(tup)...);     }      template <typename... Args>     void func(std::tuple<Args...>& tup)     {         func(tup, helper::gen_seq<sizeof...(Args)>{});     }      void act()     {         func(args);     } }; 

Here is your full program on Coliru.


Update: Here is a helper method by which specification of the template arguments aren't necessary:

template <typename F, typename... Args> Action<Args...> make_action(F&& f, Args&&... args) {     return Action<Args...>(std::forward<F>(f), std::forward<Args>(args)...); }  int main() {     auto add = make_action([] (int a, int b) { std::cout << a + b; }, 2, 3);      add.act(); } 

And again, here is another demo.

like image 170
David G Avatar answered Sep 28 '22 05:09

David G


You can use std::bind(f,args...) for this. It will generate a movable and possibly copyable object that stores a copy of the function object and of each of the arguments for later use:

#include <iostream> #include <utility> #include <functional>  template <typename... T> class Action { public:    using bind_type = decltype(std::bind(std::declval<std::function<void(T...)>>(),std::declval<T>()...));    template <typename... ConstrT>   Action(std::function<void(T...)> f, ConstrT&&... args)     : bind_(f,std::forward<ConstrT>(args)...)   { }    void act()   { bind_(); }  private:   bind_type bind_; };  int main() {   Action<int,int> add([](int x, int y)                       { std::cout << (x+y) << std::endl; },                       3, 4);    add.act();   return 0; } 

Notice that std::bind is a function and you need to store, as data member, the result of calling it. The data type of that result is not easy to predict (the Standard does not even specify it precisely), so I use a combination of decltype and std::declval to compute that data type at compile time. See the definition of Action::bind_type above.

Also notice how I used universal references in the templated constructor. This ensures that you can pass arguments that do not match the class template parameters T... exactly (e.g. you can use rvalue references to some of the T and you will get them forwarded as-is to the bind call.)

Final note: If you want to store arguments as references (so that the function you pass can modify, rather than merely use, them), you need to use std::ref to wrap them in reference objects. Merely passing a T & will create a copy of the value, not a reference.

Operational code on Coliru

like image 33
jogojapan Avatar answered Sep 28 '22 05:09

jogojapan