variadic-templates

How to get the index of a type in a variadic type pack?

谁都会走 提交于 2019-12-07 04:49:43
问题 For example template<typename T, typename... Ts> struct Index { enum {value = ???} }; and assume T is one of Ts and Ts has different types, like Index<int, int, double>::value is 0 Index<double, int, double>::value is 1 回答1: #include <type_traits> #include <cstddef> template <typename T, typename... Ts> struct Index; template <typename T, typename... Ts> struct Index<T, T, Ts...> : std::integral_constant<std::size_t, 0> {}; template <typename T, typename U, typename... Ts> struct Index<T, U,

Understanding the dots in variadic template function

戏子无情 提交于 2019-12-07 04:38:27
问题 Suppose I have the following piece of code. Which I understand to a large extent. template <class ...Args> //---> Statement A void foo_imp(const Args&... args) //---> Statement B { std::vector<int> vec = {args...}; //---> Statement C } Now I am a bit confused as to where the ... should appear before or after the name of the variable. Here is why I am confused. The statement A, given below template <class ...Args> suggests that ... is the variable type and Args is the variable name (which is

Multiple inheritance with variadic templates: how to call function for each base class?

时光毁灭记忆、已成空白 提交于 2019-12-07 04:16:01
问题 I have a diamond inheritance scheme, where the last child should be able to inherit from many different parents. A /|\ / | \ B C ... | | | * * D E Now imagine I have a class D : public B , class E : public B, public C , etc. From D I want to call the same function of all its parents, which I am guaranteed exists due to the inheritance. My thought was that I could wrap this in some variadic template. Currently I have this: template <typename T> class A { public: A(T t) : mT(t) {} virtual ~A()

Variadic templates and multiple inheritance in c++11

非 Y 不嫁゛ 提交于 2019-12-07 04:09:56
问题 i'm trying to achieve something like this: I have a templated base class which i want to inherit dynamically template<typename A, typename B> class fooBase { public: fooBase(){}; ~fooBase(){}; }; desired method: (something like this, not really sure how to do it) template <typename... Interfaces> class foo : public Interfaces... { public: foo(); ~foo(); } and my goal is to have the foo class act like this: second method: class foo() : public fooBase<uint8_t, float> , public fooBase<uint16_t,

Enable template only for specific templated class

北城余情 提交于 2019-12-07 02:05:56
问题 This question is inspired from my previous question No template parameter deduction of parameter pack. Consider following code example: #include <memory> #include <string> template<typename... FArgs> class Callback { public: class Handle{}; }; class BaseCallbackHandle { }; using TypeErasedCallbackHandle = std::unique_ptr<BaseCallbackHandle>; template<typename H> TypeErasedCallbackHandle makeTypeErasedCallbackHandle( H handle) { return {}; } int main() { Callback<int>::Handle h; std::string s;

Unpacking a typelist

狂风中的少年 提交于 2019-12-07 00:08:07
问题 Lets say I have a function that takes just a type template parameter, I cannot change it's definition/implementation. template < typename T > void do_it(); Now I have a typelist defined a usual way, can't change it either: template< typename ...Ts > struct typelist; I want to implement a function that takes a typelist, and runs do_it() on every type: template< typename List > void do_them(); The only solution I found up 'till now is: template< typename T > void do_them_impl() { do_it<T>(); }

Implementing a std::array-like container with a C++11 initializer_list

北战南征 提交于 2019-12-06 23:01:30
问题 The only and imo very inconvenient caveat of std::array is that it can't deduce its size from the initializer list like built-in C arrays, it's size must be passed as a template. Is it possible to implement a std::array-like container (a thin wrapper around a built-in C array) with a C++11 initializer_list? I ask because, unlike std::array, it would automatically deduce the size of the array from the initializer list which is a lot more convenient. For example: // il_array is the hypothetical

Dependent non-type parameter packs: what does the standard say?

可紊 提交于 2019-12-06 22:58:49
问题 I think the following code is well-formed: template< typename T > using IsSigned = std::enable_if_t< std::is_signed_v< T > >; template< typename T, IsSigned< T >... > T myAbs( T val ); Others say that it is ill-formed, because §17.7 (8.3) of the C++17 standard: Knowing which names are type names allows the syntax of every template to be checked. The program is ill-formed, no diagnostic required, if: (...) every valid specialization of a variadic template requires an empty template parameter

Applying multiple tuples to the same function (i.e. `apply(f, tuples…)`) without recursion or `tuple_cat`

白昼怎懂夜的黑 提交于 2019-12-06 22:21:21
问题 std::experimental::apply has the following signature: template <class F, class Tuple> constexpr decltype(auto) apply(F&& f, Tuple&& t); It basically invokes f by expanding t 's elements as the arguments. I would like something that does the exact same thing, but with multiple tuples at the same time: template <class F, class... Tuples> constexpr decltype(auto) multi_apply(F&& f, Tuples&&... ts); Example usage: std::tuple t0{1, 2, 3}; std::tuple t1{4, 5, 6}; auto sum = [](auto... xs){ return

Boost bind placeholder argument equal to the number of Variadic Template arguments

ⅰ亾dé卋堺 提交于 2019-12-06 20:18:58
问题 I want to know if it is possible to use the number of arguments passed to a variadic template as placeholder in a boost::bind call. Something like this: template <typename ... Args> boost::bind(&function, this, anArg, _1)); //If Args count equals 1 boost::bind(&function, this, anArg, _1, _2)); //If Args count equals 2 boost::bind(&function, this, anArg, _1, _2, _3)); //If Args count equals 3 Is this possible? Thank you 回答1: There definitely is a way with partial specialization. your variadic