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

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>() instead,… Read More Call template-function with types in a tuple (not values)

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

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, std::string… 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

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> auto… Read More Slicing a vector of tuples into a tuple of std::optional

determining variadic template arguments are compile time

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 write… Read More determining variadic template arguments are compile time

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

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 overloads:… Read More How to overload variadic template function based on size of parameter pack in C++

Why the base case with no template arguments for variadic tuple is not working?

As an exercise, I’m trying to define a variadic template for a Tuple but I found that the base case with no elements is not working. template <typename Head, typename… Tail> struct Tuple : Tuple<Tail…> { Tuple(const Head& head, const Tail&… tail) : Base{tail…}, m_head{head} {} private: using Base = Tuple<Tail…>; Head m_head; }; template… Read More Why the base case with no template arguments for variadic tuple is not working?

Why do variadic templates fail to find an appropriate constructor?

If you look at main you’ll see I can call write with 4, t2 (or t) but not both in the same call, why? Is there anything I can do outside of breaking them up into seperate calls? template<class T> struct Test { T data; constexpr Test()=default; Test(Test&)=delete; void Write() { } template<typename…Args> void Write(unsigned… Read More Why do variadic templates fail to find an appropriate constructor?

C++ variadic template: typeid of it, way to optimize

So, I learn variadic templates and usage of it. Now I made that code below. The question is does some other methode exist for getting type of "Params" without any arrays or inilialized_list? template<class Type, class… Params> void InsertInVector(std::vector<Type>& v, const Params&… params) { const auto variadic = {typeid(Params).name()…}; if (typeid(Type).name() != *variadic.begin()) { throw… Read More C++ variadic template: typeid of it, way to optimize

Expand variadic template template parameters for use in e.g. std::variant<T…>

This will be a hard nut to crack. I don’t even know if it’s possible. My goal is to create a receive function that listens to multiple queues and pastes the object received via the particular queue (that responds first) to the stack in the return statement. This will be done via std::variant. The tricky… Read More Expand variadic template template parameters for use in e.g. std::variant<T…>