Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a template function with varargs template arguments, like this

template<typename Args...> void ascendingPrint(Args... args) { /* ... */ } 

And I want to write

template<typename Args...> void descendingPrint(Args... args) {   /* implementation using ascendingPrint()? */ } 

How do I reverse the order of the parameter-pack args before passing it along, i.e. in pseudo-code:

template<typename Args...> void descendingPrint(Args... args) {   ascendingPrint( reverse(args) ); } 
like image 889
towi Avatar asked Apr 09 '13 14:04

towi


1 Answers

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 for generating indices), so no risk of run-time overhead even in case the compiler would not manage to inline the recursive function calls (which is unlikely anyway, so this is more of an academic argument).

Moreover, this solution is general, in that you can use it as a header-only library to invoke your functions with reversed arguments and with minimum effort: descending_print() should be just a minimal thin wrapper around ascending_print().

Here is how it should look like:

MAKE_REVERT_CALLABLE(ascending_print)  template<typename... Args> void descending_print(Args&&... args) {     revert_call(REVERT_ADAPTER(ascending_print), std::forward<Args>(args)...); }  

What follows is a presentation of the implementation.


First step: reverting a type sequence


Here is a simple way to revert a type sequence:

#include <tuple> #include <type_traits>  template<typename, typename> struct append_to_type_seq { };  template<typename T, typename... Ts> struct append_to_type_seq<T, std::tuple<Ts...>> {     using type = std::tuple<Ts..., T>; };  template<typename... Ts> struct revert_type_seq {     using type = std::tuple<>; };  template<typename T, typename... Ts> struct revert_type_seq<T, Ts...> {     using type = typename append_to_type_seq<         T,         typename revert_type_seq<Ts...>::type         >::type; }; 

A small test program:

int main() {     static_assert(         std::is_same<             revert_type_seq<char, int, bool>::type,             std::tuple<bool, int, char>             >::value,         "Error"         ); } 

And a live example.


Second step: reverting a tuple


The next step consists in reverting a tuple. Given the usual indices trick machinery:

template <int... Is> struct index_list { };  namespace detail {     template <int MIN, int N, int... Is>     struct range_builder;      template <int MIN, int... Is>     struct range_builder<MIN, MIN, Is...>     {         typedef index_list<Is...> type;     };      template <int MIN, int N, int... Is>     struct range_builder : public range_builder<MIN, N - 1, N - 1, Is...>     { }; }  template<int MIN, int MAX> using index_range = typename detail::range_builder<MIN, MAX>::type; 

Together with the functions defined above, a tuple can easily be reverted this way:

template<typename... Args, int... Is> typename revert_type_seq<Args...>::type revert_tuple(std::tuple<Args...> t, index_list<Is...>) {     using reverted_tuple = typename revert_type_seq<Args...>::type;      // Forwarding machinery that handles both lvalues and rvalues...     auto rt = std::forward_as_tuple(             std::forward<                 typename std::conditional<                     std::is_lvalue_reference<                         typename std::tuple_element<Is, reverted_tuple>::type                         >::value,                     typename std::tuple_element<Is, reverted_tuple>::type,                     typename std::remove_reference<                         typename std::tuple_element<Is, reverted_tuple>::type                         >::type                     >::type                 >(std::get<sizeof...(Args) - Is - 1>(t))...         );      return rt; }  template<typename... Args> typename revert_type_seq<Args...>::type revert_tuple(std::tuple<Args...> t) {     return revert_tuple(t, index_range<0, sizeof...(Args)>()); } 

Here is a simple test program:

#include <iostream>  int main() {     std::tuple<int, int, char> t(42, 1729, 'c');     auto rt = revert_tuple(t);      std::cout << std::get<0>(rt) << " "; // Prints c     std::cout << std::get<1>(rt) << " "; // Prints 1729     std::cout << std::get<2>(rt) << " "; // Prints 42 } 

Here is a live example.


Third step: reverting a function's arguments


The final step consists in unpacking the tuple when calling our target function. Here is another generic utility to save us a couple of lines:

template<typename... Args> typename revert_type_seq<Args...>::type make_revert(Args&&... args) {     auto t = std::forward_as_tuple(std::forward<Args>(args)...);     return revert_tuple(t); } 

The above function creates a tuple whose elements are the arguments provided, but in reverse order. We are not ready to define our target:

template<typename T> void ascending_print(T&& t) {     std::cout << std::forward<T>(t) << " "; }  template<typename T, typename... Args> void ascending_print(T&& t, Args&&... args) {     ascending_print(std::forward<T>(t));     ascending_print(std::forward<Args>(args)...); } 

The above function(s) prints all the arguments provided. And here is how we could write descending_print():

template<typename T, int... Is> void call_ascending_print(T&& t, index_list<Is...>) {     ascending_print(std::get<Is>(std::forward<T>(t))...); }  template<typename... Args> void descending_print(Args&&... args) {     call_ascending_print(make_revert(std::forward<Args>(args)...),          index_range<0, sizeof...(Args)>()); } 

A simple test case again:

int main() {     ascending_print(42, 3.14, "Hello, World!");     std::cout << std::endl;     descending_print(42, 3.14, "Hello, World!"); } 

And of course a live example.


Final step: simplification


The above solution may be non-trivial to understand, but it can be made trivial to use, and quite flexible. Given a couple of generic functions:

template<typename F, typename... Args, int... Is> void revert_call(F&& f, index_list<Is...>, Args&&... args) {     auto rt = make_revert(std::forward<Args>(args)...);     f(std::get<Is>(rt)...); }  template<typename F, typename... Args> void revert_call(F&& f, Args&&... args) {     revert_call(f, index_range<0, sizeof...(Args)>(),                  std::forward<Args>(args)...); } 

And a couple of macro definitions (I couldn't find a way to create an overload set for a function template, sorry):

#define MAKE_REVERT_CALLABLE(func) \     struct revert_caller_ ## func \     { \         template<typename... Args> void operator () (Args&&... args) \         { func(std::forward<Args>(args)...); } \     };  #define REVERT_ADAPTER(func) \     revert_caller_ ## func() 

It becomes really easy to adapt any function for being called with arguments in reverse order:

MAKE_REVERT_CALLABLE(ascending_print)  template<typename... Args> void descending_print(Args&&... args) {     revert_call(REVERT_ADAPTER(ascending_print), std::forward<Args>(args)...); }  int main() {     ascending_print(42, 3.14, "Hello, World!");     std::cout << std::endl;     descending_print(42, 3.14, "Hello, World!"); } 

To conclude, as usual, a live example.

like image 133
Andy Prowl Avatar answered Oct 01 '22 23:10

Andy Prowl