fork(1) download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. using namespace std;
  5.  
  6. int f()
  7. {
  8. cout << "f" << endl;
  9. return 0;
  10. }
  11.  
  12. struct F {
  13. int f()
  14. {
  15. cout << "F::f" << endl;
  16. return 0;
  17. }
  18. };
  19.  
  20. template <typename Function>
  21. typename std::result_of<typename std::remove_reference<Function>::type>::type
  22. call_f(Function&& f)
  23. {
  24. return f();
  25. }
  26.  
  27. template <typename Function, class Class>
  28. typename std::result_of<Function>::type
  29. call_mf(Function&& f, Class* instance)
  30. {
  31. return instance->*f();
  32. }
  33.  
  34. int main()
  35. {
  36. call_f(f);
  37.  
  38. F instance;
  39.  
  40. call_fm(&F::f, &instance);
  41.  
  42. return 0;
  43. }
  44.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:36:10: error: no matching function for call to ‘call_f(int (&)())’
  call_f(f);
          ^
prog.cpp:36:10: note: candidate is:
prog.cpp:22:1: note: template<class Function> typename std::result_of<typename std::remove_reference< <template-parameter-1-1> >::type>::type call_f(Function&&)
 call_f(Function&& f)
 ^
prog.cpp:22:1: note:   template argument deduction/substitution failed:
prog.cpp: In substitution of ‘template<class Function> typename std::result_of<typename std::remove_reference< <template-parameter-1-1> >::type>::type call_f(Function&&) [with Function = int (&)()]’:
prog.cpp:36:10:   required from here
prog.cpp:22:1: error: no type named ‘type’ in ‘class std::result_of<int()>’
prog.cpp:40:26: error: ‘call_fm’ was not declared in this scope
  call_fm(&F::f, &instance);
                          ^
stdout
Standard output is empty