fork download
  1. #include <functional>
  2. #include <iostream>
  3.  
  4. std::function<int(int, int)> sum = [](int a, int b) { return a + b; };
  5.  
  6. // create a "before" hook
  7. template<typename Functor, typename Hook>
  8. Functor hook_before(const Functor & original, Hook hook)
  9. {
  10. // not legal, but illustrates what I want to achive
  11. template<typename Args ...args>
  12. return [=](Args ...args)
  13. {
  14. hook();
  15. original(args...);
  16. };
  17. }
  18.  
  19. int main()
  20. {
  21. std::cout << sum(3, 4) << std::endl;
  22.  
  23. auto myhook = []() { std::cout << "Calculating sum" << std::endl; };
  24. auto hooked_sum = hook_before(sum, myhook);
  25. hooked_sum(3, 4);
  26. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'Functor hook_before(const Functor&, Hook)':
prog.cpp:11:5: error: expected primary-expression before 'template'
prog.cpp:11:5: error: expected ';' before 'template'
stdout
Standard output is empty