Call a function using expansion pack on a same function

Advertisements I would like to be able to call a function over any number of arguments, with each argument’s value supplied by another function: template <typename T, std::size_t N> class MyClass { public: void doSomething() { doSomethingProxy(std::make_index_sequence<N>()); } private: template <std::size_t… I> void doSomethingProxy(std::index_sequence<I…>) { m_obj.doSomething({get()…}); // T::doSomething() expects an object whose constructor has N… Read More Call a function using expansion pack on a same function

How best to write the base case of this recursive template?

Advertisements I’m trying to write a template function that has this behavior: // count_template_params<>() == 0 // count_template_params<int>() == 1 // count_template_params<int, int>() == 2 // count_template_params<int, int, int>() == 3 // etc… This is the closest I’ve been able to achieve: #include <iostream> template<int DUMMY> constexpr int count_template_params() { return 0; } template<int DUMMY,… Read More How best to write the base case of this recursive template?

Expand variadic template fold expression

Advertisements template<typename… Ts> using Type = std::variant<std::shared_ptr<T1>, std::shared_ptr<T2>, std::shared_ptr<Tn>>; How I can do this using variadic templates and fold expressions? I try something like this: template<typename… Ts> using Type = std::variant<((std::shared_ptr<Ts>), …)>;` but it doesn’t compile. >Solution : The following compiled for me template<typename… Ts> using Type = std::variant<std::shared_ptr<Ts>…>; As stated here A pattern followed… Read More Expand variadic template fold expression

Loop on variadic templates types at runtime

Advertisements I migrate a code from C++11 to C++17. In that code I do something like this (non-valid) code: template <typename… Ts> std::vector<std::size_t> store_types() { std::vector<std::size_t> t; for(typename T : Ts) t.push_back(typeid(T).hash_code()); return t; } For the moment, my implementation is the following: template <typename Unused> void add_types_in(std::vector<std::size_t>&) {} template <typename Unused, typename First, typename…… Read More Loop on variadic templates types at runtime

C++ function strprint(expr, expr, expr…) almost works, but not quite, why?

Advertisements Here’s a small C++ ‘strprint’ function that seems to mostly work: #include <sstream> #include <iostream> // send_to_stream: send all arguments to the specified stream. inline void send_to_stream(std::ostream &os) {} template <typename ARG, typename… REST> inline void send_to_stream(std::ostream &os, const ARG &arg, const REST & … rest) { os << arg; send_to_stream(os, rest…); } //… Read More C++ function strprint(expr, expr, expr…) almost works, but not quite, why?

Call template-function with types in a tuple (not values)

Advertisements I would like to call a template function with no arguments for each type in a tuple. The code below shows exactly what the intention is. My solution involves making a dummy instance of DataGroup(). I’d like to avoid this, as the types may not have a default constructor. I’ve attempted to use std::declval<DataGroup>()… Read More Call template-function with types in a tuple (not values)

Pass a function pointer with variable parameters as an argument in C++

Advertisements I’m trying to pass a function, with a variable amount of parameters, as an argument to my function baz(). I’m not really sure how to go about doing this, but hopefully the example I’ve provided below shows my current thought-process. #include <iostream> template<typename… Ts> void foo(void (*func)(Ts…)){ func(Ts…); } void bar(int a, int b,… Read More Pass a function pointer with variable parameters as an argument in C++

Slicing a vector of tuples into a tuple of std::optional

Advertisements I’m having trouble slicing a tuple. I want to output a tuple with std::optional, so that if the vectors are of an uneven length, the tuple is padded with empty std::optional objects. An example of my implementation is below, but I’m running into a roadblock, which is commented in the if-statements template <typename… Types>… Read More Slicing a vector of tuples into a tuple of std::optional

determining variadic template arguments are compile time

Advertisements Let’s assume i want to define a type that depends on some types: struct TimerPump{}; struct GuiPump{}; struct NetworkPump{}; template<class… Pumps> class DispatcherT{}; using Dispatcher = DispatcherT< TimerPump, GuiPump, NetworkPump >; I would like to make the gui and network pumps be optional. Either one might be needed or both or none. I could… Read More determining variadic template arguments are compile time

How to overload variadic template function based on size of parameter pack in C++

Advertisements I’m sorry if this question is basic. Let’s say I have a function template that took in a parameter pack of arguments. How would I overload it so that there might be a specialization for 2 arguments getting passed in, 3 arguments passed in, etc. >Solution : For small parameter packs, I’d use non-variadic… Read More How to overload variadic template function based on size of parameter pack in C++