fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. template<size_t... indexes>
  5. struct index_tuple {};
  6.  
  7. template<size_t head, size_t... indexes>
  8. struct index_tuple<head, indexes...> {
  9. typedef typename index_tuple<head-1, head-1, indexes...>::type type;
  10. };
  11.  
  12. template<size_t... indexes>
  13. struct index_tuple<0, indexes...> {
  14. typedef index_tuple<indexes...> type;
  15. };
  16.  
  17. template<typename... Args>
  18. struct index_tuple_maker {
  19. typedef typename index_tuple<sizeof...(Args)>::type type;
  20. };
  21.  
  22.  
  23. template< typename ... Args >
  24. class Message {
  25. public:
  26. Message( Args&& ... args ) {
  27. mArgs = std::make_tuple( args ... );
  28. }
  29.  
  30. std::tuple< Args ... > mArgs;
  31. typedef std::function< void ( Args ... ) > HandlerType;
  32.  
  33. void Consume( const HandlerType &handler ) {
  34. Consume(handler, typename index_tuple_maker<Args...>::type());
  35. }
  36.  
  37. template<size_t... ns>
  38. void Consume(const HandlerType &handler, index_tuple<ns...>) {
  39. handler(std::get<ns>(mArgs)...);
  40. }
  41. };
  42.  
  43.  
  44. int main() {
  45. Message<int, int> msg(1, 2);
  46.  
  47. msg.Consume( [] ( int i, int j ) {
  48. std::cout << i << ',' << j << '\n';
  49. });
  50. }
  51.  
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
1,2