template <typename Type, typename ReturnType, typename MemFuncType>
struct mem_fun_ptr_t
{
    MemFuncType func;
public:
    mem_fun_ptr_t(MemFuncType f) :
      func(f) {}
    ReturnType operator () (Type *p) const { return (p->*func)(); }
};

// non-const version
template <typename T, typename R>
mem_fun_ptr_t<T, R, R (T::*)()> mem_fun_ptr(R (T::*Func)())
{
    return mem_fun_ptr_t<T, R, R (T::*)()>(Func);
}

// const version
template <typename T, typename R>
mem_fun_ptr_t<const T, R, R (T::*)() const> mem_fun_ptr(R (T::*Func)() const)
{
    return mem_fun_ptr_t<const T, R, R (T::*)() const>(Func);
}

#include <iostream>
#include <string>

int main()
{
    std::string str = "Hello";
    auto x = mem_fun_ptr(&std::string::length);
    std::cout << x(&str) << std::endl;
    const std::string const_str = str + ", world!";
    std::cout << x(&const_str) << std::endl;
    return 0;
}
