How to reverse the order of arguments of a variadic template function?

Overall approach and usage The overal approach consists in packing the arguments into an std::tuple of references, exploiting the perfect forwarding machinery of std::forward_as_tuple(). This means that, at run-time, you should incur in very small overhead and no unnecessary copy/move operations. Also, the framework does not use recursion (apart from compile-time recursion, which is unavoidable … Read more

c++ lambdas how to capture variadic parameter pack from the upper scope

Perfect capture in C++20 template <typename … Args> auto f(Args&& … args){ return [… args = std::forward<Args>(args)]{ // use args }; } C++17 and C++14 workaround In C++17 we can use a workaround with tuples: template <typename … Args> auto f(Args&& … args){ return [args = std::make_tuple(std::forward<Args>(args) …)]()mutable{ return std::apply([](auto&& … args){ // use args … Read more

Parameter pack must be at the end of the parameter list… When and why?

It is valid for function templates but only when argument deduction can help the compiler resolve the template parameters, as it stands your function template example is virtually useless because template<typename T, typename… Args, typename S> void fn() { } int main() { fn<int, int, int>(); } test.cpp: In function ‘int main()’: test.cpp:2:32: error: no … Read more

How to create the Cartesian product of a type list?

A nice clean version I think: cross_product.cpp: #include “type_printer.hpp” #include <iostream> template<typename …Ts> struct type_list {}; template<typename T1, typename T2> struct pair {}; // Concatenation template <typename … T> struct concat; template <typename … Ts, typename … Us> struct concat<type_list<Ts…>, type_list<Us…>> { typedef type_list<Ts…, Us…> type; }; // Cross Product template <typename T, typename U> … Read more

Is it possible to “store” a template parameter pack without expanding it?

Another approach, which is slightly more generic than Ben’s, is as follows: #include <tuple> template <typename… Args> struct variadic_typedef { // this single type represents a collection of types, // as the template arguments it took to define it }; template <typename… Args> struct convert_in_tuple { // base case, nothing special, // just use the … Read more

What is the meaning of “… …” token? i.e. double ellipsis operator on parameter pack

Every instance of that oddity is paired with a case of a regular single ellipsis. template<typename _Res, typename… _ArgTypes> struct _Weak_result_type_impl<_Res(_ArgTypes…)> { typedef _Res result_type; }; template<typename _Res, typename… _ArgTypes> struct _Weak_result_type_impl<_Res(_ArgTypes……)> { typedef _Res result_type; }; template<typename _Res, typename… _ArgTypes> struct _Weak_result_type_impl<_Res(_ArgTypes…) const> { typedef _Res result_type; }; template<typename _Res, typename… _ArgTypes> struct _Weak_result_type_impl<_Res(_ArgTypes……) … Read more

How to make generic computations over heterogeneous argument packs of a variadic template function?

Since I was not happy with what I found, I tried to work out a solution myself and ended up writing a small library which allows formulating generic operations on argument packs. My solution has the following features: Allows iterating over all or some elements of an argument pack, possibly specified by computing their indices … Read more

How to store variadic template arguments?

To accomplish what you want done here, you’ll have to store your template arguments in a tuple: std::tuple<Ts…> args; Furthermore, you’ll have to change up your constructor a bit. In particular, initializing args with an std::make_tuple and also allowing universal references in your parameter list: template <typename F, typename… Args> Action(F&& func, Args&&… args) : … Read more

Check at Compile-Time if Template Argument is void

You can use a helper class to fine tune specializations: template <typename F> struct wrapper {}; template <typename Res, typename… Args> struct wrapper<Res(Args…)> { static Res wrap(Res (WINAPI *f)(Args…), Args&& args…) { Res r = f(std::forward<Args>(args)…); // Blah blah return r; } }; template <typename… Args> struct wrapper<void(Args…)> { static void wrap(void (WINAPI *f)(Args…), Args&& … Read more