makiolo

Calling lambda after variadic templates

Dec 15th, 2015
115
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <tuple>
  3.  
  4. template <int... Is>
  5. struct seq
  6. {
  7. };
  8.  
  9. template <int N, int... Is>
  10. struct gens : gens<N - 1, N - 1, Is...>
  11. {
  12. };
  13.  
  14. template <int... Is>
  15. struct gens<0, Is...> : seq<Is...>
  16. {
  17. };
  18.  
  19. template <int N, typename ... Args, int... S>
  20. void _foo(const std::tuple<Args...>& t, seq<S...>)
  21. {
  22.     auto&& f = std::get<N>(t);
  23.     f(std::get<S>(t)...);
  24. }
  25.  
  26. template <typename ... Args>
  27. void foo(Args&& ... args)
  28. {
  29.     _foo<sizeof...(Args)-1>(std::make_tuple(std::forward<Args>(args)...), gens<sizeof...(Args)-1>{});
  30. }
  31.  
  32. int main()
  33. {
  34.     foo(1, 2, 3, [](auto a, auto b, auto c)
  35.     {
  36.         std::cout << "a= " << a << std::endl;
  37.         std::cout << "b= " << b << std::endl;
  38.         std::cout << "c= " << c << std::endl;
  39.     });
  40.     return 0;
  41. }
RAW Paste Data