fork(1) download
  1. #include <cstddef>
  2. #include <functional>
  3. #include <utility>
  4. #include <type_traits>
  5.  
  6. template <typename C, std::size_t N>
  7. struct NaryDispatch
  8. {
  9. template <typename T, std::size_t K, typename ...Args>
  10. struct function_maker
  11. {
  12. using type = typename function_maker<T, K - 1, T, Args...>::type;
  13. };
  14.  
  15. template <typename T, typename ...Args>
  16. struct function_maker<T, 0, Args...>
  17. {
  18. using type = std::function<void(Args...)>;
  19. };
  20.  
  21. using f_type = typename function_maker<C &, N>::type;
  22.  
  23. template <typename F>
  24. NaryDispatch(F && f) : fn_(std::forward<F>(f)) {}
  25.  
  26. template <typename ...Args, typename = typename std::enable_if<sizeof...(Args) == N>::type>
  27. void run(Args &&... args)
  28. {
  29. fn_(std::forward<Args>(args)...);
  30. }
  31.  
  32. f_type fn_;
  33. };
  34.  
  35.  
  36. #include <iostream>
  37.  
  38. int main()
  39. {
  40. int x = 1, y = 2, z = 3;
  41.  
  42. NaryDispatch<int, 3> d([](int & a, int & b, int & c){
  43. std::cout << "[" << a << ", " << b << ", " << c << "]\n"; });
  44.  
  45. d.run(x, y, z);
  46. }
  47.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
[1, 2, 3]