fork download
  1. #include <type_traits>
  2. #include <iostream>
  3. #include <string>
  4. #include <typeinfo>
  5. #include <utility>
  6. #include <tuple>
  7.  
  8.  
  9. struct context{};
  10.  
  11. template<class T> auto pass(const context& c) noexcept
  12. {
  13. using arg = ::std::remove_cv_t<
  14. ::std::remove_reference_t<T>
  15. >;
  16.  
  17. std::cout << "type = " << typeid(T).name() << std::endl;
  18. return arg{};
  19. }
  20.  
  21. void some_function(int v1, bool v2, const std::string& v3) noexcept
  22. {
  23. std::cout << "call("
  24. << v1 << ", "
  25. << std::boolalpha << v2 <<" , \""
  26. << v3 <<"\" );\n";
  27. }
  28.  
  29.  
  30. template<typename F, typename ArgsTuple, std::size_t ...Indices>
  31. auto apply(F && f, ArgsTuple && args, std::index_sequence<Indices...>) {
  32. return std::forward<F>(f)(std::get<Indices>(std::forward<ArgsTuple>(args))...);
  33. }
  34.  
  35.  
  36. template<class... Args> void expand(const Args&...)
  37. {
  38. using tuple_type = std::tuple<std::remove_cv_t<decltype(pass<Args>(std::declval<context>()))>...>;
  39.  
  40. context ctx;
  41. tuple_type args { pass<Args>(ctx)... };
  42.  
  43. apply(some_function,
  44. std::move(args),
  45. std::make_index_sequence<sizeof...(Args)> {});
  46. }
  47.  
  48. int main()
  49. {
  50. expand(42, true, std::string { "answer" });
  51. }
  52.  
Success #stdin #stdout 0s 4484KB
stdin
Standard input is empty
stdout
type = i
type = b
type = NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
call(0, false , "" );