fork download
  1. #include <iostream>
  2. #include <list>
  3. #include <utility>
  4. #include <vector>
  5.  
  6. template <typename F> struct arity;
  7.  
  8. template <typename Ret, typename ...Args> struct arity<Ret(Args...)>
  9. {
  10. static constexpr std::size_t value = sizeof...(Args);
  11. };
  12.  
  13. template <typename Func, typename T, std::size_t ... Is>
  14. decltype(auto) apply(Func&& f, const std::list<T>& pars, std::index_sequence<Is...>)
  15. {
  16. std::vector<T> v(pars.rbegin(), pars.rend());
  17.  
  18. return std::forward<Func>(f)(v.at(Is)...);
  19. }
  20.  
  21. template <typename Func, typename T>
  22. decltype(auto) apply(Func&& f, const std::list<T>& pars)
  23. {
  24. constexpr std::size_t N = arity<std::remove_pointer_t<std::decay_t<Func>>>::value;
  25. return apply(std::forward<Func>(f), pars, std::make_index_sequence<N>());
  26. }
  27.  
  28. void print(int a, int b, int c, int d, int e, int f)
  29. {
  30. std::cout << a << " " << b << " "<< c << " "<< d << " "<< e << " "<< f << std::endl;
  31. }
  32.  
  33. int main()
  34. {
  35. const std::list<int> l{4, 8, 15, 16, 23, 42};
  36.  
  37. apply(print, l);
  38. }
  39.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
42 23 16 15 8 4