fork(5) download
  1. #include <iostream>
  2. #include <tuple>
  3. #include <type_traits>
  4. using namespace std;
  5.  
  6. template <typename Fnc, typename ... Types, std::size_t ... Indices>
  7. auto apply_impl(Fnc &&fnc, const std::tuple<Types...> &tuple, std::index_sequence<Indices...>)
  8. {
  9. return std::forward<Fnc>(fnc)(std::get<Indices>(tuple)...);
  10. }
  11.  
  12. template <typename Fnc, typename ... Types>
  13. auto apply(Fnc &&fnc, const std::tuple<Types...> &tuple)
  14. {
  15. return apply_impl(std::forward<Fnc>(fnc), tuple, std::index_sequence_for<Types...>());
  16. }
  17.  
  18. void foo(int a, float b, char c)
  19. {
  20. std::cout << a << ' ' << b << ' ' << c << '\n';
  21. }
  22.  
  23. int main()
  24. {
  25. auto tuple = std::make_tuple(4, 3.12, 'A');
  26. apply(foo, tuple);
  27. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
4 3.12 A