#include namespace detail { /////////////////////////////////////////////////////////////////////////////// // function_ptr_baseN /////////////////////////////////////////////////////////////////////////////// template struct function_ptr_base0 { public: typedef R (*ftype)(C *); operator ftype() { return f; } private: static R f(C *c) { return (c->*pmf)(); } }; template struct function_ptr_base1 { public: typedef R (*ftype)(C *, A1); operator ftype() { return f; } private: static R f(C *c, A1 a1) { return (c->*pmf)(a1); } }; template struct function_ptr_base2 { public: typedef R (*ftype)(C *, A1, A2); operator ftype() { return f; } private: static R f(C *c, A1 a1, A2 a2) { return (c->*pmf)(a1, a2); } }; /////////////////////////////////////////////////////////////////////////////// // function_ptr_impl /////////////////////////////////////////////////////////////////////////////// template struct function_ptr_impl; template struct function_ptr_impl { typedef R (C::*type)(); template struct base : function_ptr_base0 {}; }; template struct function_ptr_impl { typedef R (C::*type)(A1); template struct base : function_ptr_base1 {}; }; template struct function_ptr_impl { typedef R (C::*type)(A1, A2); template struct base : function_ptr_base2 {}; }; } // namespace detail /////////////////////////////////////////////////////////////////////////////// // function_ptr /////////////////////////////////////////////////////////////////////////////// template::type pmf> struct function_ptr : detail::function_ptr_impl::base {}; /////////////////////////////////////////////////////////////////////////////// // Test /////////////////////////////////////////////////////////////////////////////// class test { public: test(int v) : value(v) {} int f1() { return value; } int f2(int a) { return a+value; } int f3(int a) { return -a-value; } double f4(double a, double b) { return a+b; } private: int value; }; int main() { test t(1); function_ptr f1; function_ptr f2; function_ptr f3; function_ptr f4; std::cout << f1(&t) << '\n' // Prints "1" << f2(&t,3) << '\n' // Prints "4" << f3(&t, 3) << '\n' // Prints "-4" << f4(&t, 1.23, 2.34) << '\n'; // Prints "3.57" std::cin.ignore(); }