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