fork download
  1. #include <iostream>
  2.  
  3. template <typename Type, typename ReturnType>
  4. struct mem_fun_ptr_t
  5. {
  6. typedef ReturnType (Type::*Func)();
  7. Func func;
  8. public:
  9. mem_fun_ptr_t(Func f):
  10. func(f) {}
  11. ReturnType operator () (Type *p) { return (p->*func)(); }
  12. ReturnType operator () (Type *p) const { return (p->*func)(); }
  13. };
  14.  
  15. template <typename T, typename R>
  16. mem_fun_ptr_t<T, R> mem_fun_ptr(R (T::*Func)() const)
  17. {
  18. typedef R (T::*f)();
  19. f x = reinterpret_cast<f>(Func);
  20. return mem_fun_ptr_t<T, R>(x);
  21. }
  22.  
  23. int main()
  24. {
  25. std::string str = "Hello";
  26. auto x = mem_fun_ptr(&std::string::length);
  27. std::cout << x(&str);
  28. return 0;
  29. }
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
5