Category : variadic-templates

Is there some elegant way to express a function call depending on some n which can be derived from template arguments f(my_array[0], … , my_array[n-1]); inside a class template looking like this?: template <int… numbers> class Abraham { static constexpr std::size_t n = sizeof…(numbers); some_type my_array[n]; void foo(){ //… f(my_array[0], … , my_array[n-1]); // This ..

Read more

I have something like that based on variadic templates: class MyClass { public: template <typename… OtherTs> typename std::enable_if<sizeof…(OtherTs) == 0>::type Setup() {} template <typename T, typename…OtherTs> void Setup() { DoSomeStuff(new T()); Setup<OtherTs>(); } }; It’s working well and I can setup an instantiation of MyClass based on other classes like that myClass.Setup<A,B,C>(). What I would ..

Read more

I am trying to create a factory method that instantiates a template class by passing the constructor arguments for the component classes as template parameter packs. The ProducerConsumer template is parameterized on the types of the Producer and Consumer, which are aggregated by ProducerConsumer. Arguments to the factory function depend on the constructor arguments of ..

Read more

Assuming a variadic class which offers customization through template parameters. template <bool Option, class… Args> struct basic_my_class{}; And some aliases to simplify the class usage. template <class… Args> using my_class = basic_my_class<false, Args…>; template <class… Args> using my_other_class = basic_my_class<true, Args…>; A user can no longer instantiate the class without angled brackets. // No can ..

Read more

I want to store a function reference with a variable number of arguments in a class data member and call it later as follows: auto my_action = new ExecuteAction("Add", add, 1, 2); std::vector<ExecuteAction *> execute_actions; execute_actions.push_back(my_action); // … auto my_action_retrieved = execute_actions.at(0); my_action_retrieved->execute(); Note that add() is e.g. defined as follows: void add(int a, int ..

Read more