fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <functional>
  4.  
  5. template<typename ReturnType, typename... Args>
  6. class Signal {
  7. std::vector<std::function<ReturnType(Args...)>> function;
  8. public:
  9. template<typename... Args2>
  10. ReturnType operator()(Args2&&... args2) {
  11. ReturnType ret;
  12. for (auto& func : function)
  13. ret = func(std::forward<Args2>(args2)...);
  14. return ret;
  15. }
  16.  
  17. template<typename Func>
  18. void func(Func const &func) {
  19. function.push_back(std::function<ReturnType(Args...)>(func));
  20. }
  21.  
  22. template<typename Class, typename Instance>
  23. void mfunc(ReturnType(Class::*func)(Args...), Instance &instance) {
  24. function.push_back([&instance, func](Args&&... args) {
  25. return (instance.*func)(std::forward<Args>(args)...);
  26. });
  27. }
  28.  
  29. };
  30.  
  31. struct foo {
  32. int bar(int i, double d) { std::cout << i << ' ' << d; return 0; }
  33. };
  34.  
  35. int main()
  36. {
  37. Signal<int, int, double> sig;
  38. foo f;
  39. sig.mfunc(&foo::bar, f);
  40. sig(5, 3.);
  41. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
5 3