fork download
  1. #include <functional>
  2. #include <iostream>
  3.  
  4. template <typename...Args>
  5. class Dispatcher {
  6. typedef typename std::function<void(Args...)> Fn;
  7.  
  8. public:
  9. Dispatcher(Fn f) : m_function(std::move(f)) {}
  10.  
  11. void operator()(Args...args) {
  12. m_function(std::forward<Args>(args)...);
  13. }
  14.  
  15. private:
  16. Fn m_function;
  17. };
  18.  
  19. template <typename C, size_t N, typename...Args>
  20. struct NaryDispatch_ {
  21. using type = typename NaryDispatch_<C, N-1, Args..., C&>::type;
  22. };
  23. template <typename C, typename...Args>
  24. struct NaryDispatch_<C, 0, Args...> {
  25. using type = Dispatcher<Args...>;
  26. };
  27.  
  28. template <typename C, size_t N>
  29. using NaryDispatch = typename NaryDispatch_<C, N>::type;
  30.  
  31.  
  32. int main() {
  33. NaryDispatch<const int, 3> f{[](const int& a, const int& b, const int& c){
  34. std::cout << a << ' ' << b << ' ' << c << '\n';
  35. }};
  36. f(42, 13, 11);
  37. }
  38.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
42 13 11