fork download
  1. #include <tuple>
  2. #include <utility>
  3.  
  4. namespace detail
  5. {
  6. template<int... Is>
  7. struct seq { };
  8.  
  9. template<int N, int... Is>
  10. struct gen_seq : gen_seq<N - 1, N - 1, Is...> { };
  11.  
  12. template<int... Is>
  13. struct gen_seq<0, Is...> : seq<Is...> { };
  14.  
  15. template<typename F, typename... Ts, int... Is>
  16. void call_with_tuple(F&& f, std::tuple<Ts...> const& t, seq<Is...>)
  17. {
  18. (std::forward<F>(f))(std::get<Is>(t)...);
  19. }
  20. }
  21.  
  22. template<typename F, typename... Ts>
  23. void call_with_tuple(F&& f, std::tuple<Ts...> const& t)
  24. {
  25. detail::call_with_tuple(std::forward<F>(f), t, detail::gen_seq<sizeof...(Ts)>());
  26. }
  27.  
  28. #include <iostream>
  29. #include <functional>
  30.  
  31. template< typename ... Args >
  32. class Message {
  33. public:
  34. Message( Args&& ... args ) {
  35. mArgs = std::make_tuple( args ... );
  36. }
  37.  
  38. std::tuple< Args ... > mArgs;
  39. typedef std::function< void ( Args ... ) > HandlerType;
  40.  
  41. void Consume( HandlerType handler ) {
  42. call_with_tuple(handler, mArgs);
  43. }
  44. };
  45.  
  46.  
  47. int main()
  48. {
  49. // Testing code
  50. Message<int, int> msg(1, 2);
  51.  
  52. msg.Consume( [] ( int i, int j ) {
  53. std::cout << i << ',' << j << '\n';
  54. });
  55. }
  56.  
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
1,2