fork download
  1. #include <type_traits>
  2.  
  3. using namespace std;
  4.  
  5. int foo(int i) {
  6. return i;
  7. }
  8.  
  9.  
  10. struct A
  11. {
  12. int foo(int i) { return i; }
  13.  
  14. auto bar(int x) -> std::result_of<decltype(&A::foo)(A, int)>::type
  15. { // see: http://n...content-available-to-author-only...o.edu/2014/ref/cppreference/en/cpp/types/result_of.html
  16. return x;
  17. }
  18.  
  19. template<typename Func, typename... Args>
  20. auto call(Func func, Args&&... args) const
  21. -> typename std::result_of< decltype(func)(A*, Args...)>::type
  22. { this->*func(args...);
  23.  
  24. }
  25. };
  26.  
  27.  
  28.  
  29.  
  30. int main() {
  31. static_assert(std::is_same<decltype(foo(123)), int>::value, "");
  32.  
  33.  
  34. A a;
  35. static_assert(std::is_same<decltype(a.foo(123)), decltype(a.bar(123))>::value, "");
  36. static_assert(std::is_same<decltype(a.foo(123)), decltype(a.bar(123))>::value, "");
  37.  
  38.  
  39. auto func = &A::foo;
  40. static_assert(std::is_same<decltype(a.foo(123)), decltype(a.call(func, 123))>::value, "");
  41. static_assert(std::is_same<decltype(a.foo(123)),
  42. std::result_of<decltype(&A::foo)(A, int)>::type
  43. >::value, "");
  44.  
  45.  
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0s 3292KB
stdin
Standard input is empty
stdout
Standard output is empty