check variadic templates parameters for uniqueness

Passing a pointer to base_all<U…> merely requires the existence of a declaration of base_all<U…>. Without attempting the to access the definition, the compiler won’t detect that the type is actually ill-defined. One approach to mitigate that problem would be to use an argument which requires a definition of base_all<U…>, e.g.: template< class …T> struct base_all … Read more

recursive variadic template to print out the contents of a parameter pack

There’s actually a very elegant way to end the recursion: template <typename Last> std::string type_name () { return std::string(typeid(Last).name()); } template <typename First, typename Second, typename …Rest> std::string type_name () { return std::string(typeid(First).name()) + ” ” + type_name<Second, Rest…>(); } I initially tried template <typename Last> and template <typename First, typename …Rest> but that was … Read more

C++11: I can go from multiple args to tuple, but can I go from tuple to multiple args? [duplicate]

Try something like this: // implementation details, users never invoke these directly namespace detail { template <typename F, typename Tuple, bool Done, int Total, int… N> struct call_impl { static void call(F f, Tuple && t) { call_impl<F, Tuple, Total == 1 + sizeof…(N), Total, N…, sizeof…(N)>::call(f, std::forward<Tuple>(t)); } }; template <typename F, typename Tuple, … Read more

How do I get the argument types of a function pointer in a variadic template class?

You can write function_traits class as shown below, to discover the argument types, return type, and number of arguments: template<typename T> struct function_traits; template<typename R, typename …Args> struct function_traits<std::function<R(Args…)>> { static const size_t nargs = sizeof…(Args); typedef R result_type; template <size_t i> struct arg { typedef typename std::tuple_element<i, std::tuple<Args…>>::type type; }; }; Test code: struct … Read more

How to call a function on all variadic template args?

C++17 fold expression (f(args), …); If you call something that might return an object with overloaded comma operator use: ((void)f(args), …); Pre-C++17 solution The typical approach here is to use a dumb list-initializer and do the expansion inside it: { print(Args)… } Order of evaluation is guaranteed left-to-right in curly initialisers. But print returns void … Read more

Pretty-print std::tuple

Yay, indices~ namespace aux{ template<std::size_t…> struct seq{}; template<std::size_t N, std::size_t… Is> struct gen_seq : gen_seq<N-1, N-1, Is…>{}; template<std::size_t… Is> struct gen_seq<0, Is…> : seq<Is…>{}; template<class Ch, class Tr, class Tuple, std::size_t… Is> void print_tuple(std::basic_ostream<Ch,Tr>& os, Tuple const& t, seq<Is…>){ using swallow = int[]; (void)swallow{0, (void(os << (Is == 0? “” : “, “) << std::get<Is>(t)), … Read more