廢話不多說,上來貼代碼最實在,哈哈! 以下代碼量有點多,不過這都是在下一手一手敲出來的,小巧好用,把以下代碼複製出來,放到相應的hpp文件即可,VS,GCC下均能編譯通過 接下來是traits庫的完整代碼 ...
廢話不多說,上來貼代碼最實在,哈哈!
以下代碼量有點多,不過這都是在下一手一手敲出來的,小巧好用,把以下代碼複製出來,放到相應的hpp文件即可,VS,GCC下均能編譯通過
1 #include<iostream> 2 #include "../../traits/traits.hpp" 3 4 5 using namespace std; 6 7 8 int show(char i, int j) 9 { 10 return 1; 11 } 12 13 struct Stu 14 { 15 int show(char i, int j) 16 { 17 return 1; 18 } 19 }; 20 21 int main() 22 { 23 // 變數;類型萃取 24 traits::type_traits<int>::const_value_type i = 5; // 通過traits::type_traits可以萃取到int類型的各種"衍生"類型,如指針什麼的 25 26 // 以下是C函數的相關萃取 27 traits::function_traits<int(char, int)>::FunctionP_Type func_ptr = show; // 得到指針 28 func_ptr('a', 4); 29 30 cout << traits::function_traits<int(char, int)>::arity << endl; // 萃取出參數個數 31 cout << typeid(traits::function_traits<int(char, int)>::arg2).name() << endl;//萃取出參數1的類型 32 cout << typeid(traits::function_traits<int(char, int)>::arg1).name() << endl;//萃取出參數2的類型 33 34 // 以下是類成員函數的相關萃取 35 traits::mfunction_traits<int(Stu::*)(char, int)>::MFunctionP_Type mfunc_ptr = &Stu::show; // 得到指針 36 Stu stu; 37 ((&stu)->*mfunc_ptr)('a', 4); 38 39 cout << typeid(traits::mfunction_traits<int(Stu::*)(char, int)>::arg2).name() << endl;//萃取出參數1的類型 40 cout << typeid(traits::mfunction_traits<int(Stu::*)(char, int)>::arg1).name() << endl;//萃取出參數2的類型 41 cout << typeid(traits::mfunction_traits<int(Stu::*)(char, int)>::result_type).name() << endl;//萃取出返回值 42 cout << typeid(traits::mfunction_traits<int(Stu::*)(char, int)>::class_type).name() << endl;//萃取出類類型 43 44 // 以下展示從C函數指針轉為類成員函數指針的技巧 45 traits::fun_to_mem_converter<int(char, int), Stu>::MFunctionP_Type mfunc_ptr2 = mfunc_ptr; 46 47 // 以下展示從類成員函數指針轉為C函數指針的技巧 48 traits::mem_to_fun_converter<int(Stu::*)(char, int)>::FunctionP_Type func_ptr2 = func_ptr; 49 return 0; 50 }
接下來是traits庫的完整代碼
// traits.hpp #ifndef TRAITS_INCLUDE #define TRAITS_INCLUDE #include "function_traits.hpp" #include "mfunction_traits.hpp" #include "fun_to_mem_converter.hpp" #include "mem_to_fun_converter.hpp" #include "type_traits.hpp" #include "pointer_integer_traits.hpp" #endif
// traits_config.hpp #ifndef TRAITS_CONFIG_INCLUDE #define TRAITS_CONFIG_INCLUDE #define NAMESPACE_TRAITS_BEGIN namespace traits{ #define NAMESPACE_TRAITS_END } #endif
//type_traits.hpp #ifndef TYPE_TRAITS_INCLUDE #define TYPE_TRAITS_INCLUDE #include "traits_config.hpp" NAMESPACE_TRAITS_BEGIN template<typename T> struct type_traits { typedef T value_type; typedef const T const_value_type; typedef T* pointer_type; typedef const T* const_pointer_type; typedef T** pointer_pointer_type; typedef const T** const_pointer_pointer_type; typedef T& reference_type; typedef const T& const_reference_type; typedef T*& pointer_reference_type; typedef const T*& const_pointer_reference_type; static bool is_reference(){return false;}; static bool is_pointer(){return false;}; static bool is_value(){return true;}; static bool is_pointer_reference(){return false;} static bool is_pointer_pointer(){return false;} static bool is_const_reference(){return false;}; static bool is_const_pointer(){return false;}; static bool is_const_value(){return false;}; static bool is_const_pointer_reference(){return false;} static bool is_const_pointer_pointer(){return false;} }; template<typename T> struct type_traits<const T> : public type_traits<T> { static bool is_const_reference(){return false;}; static bool is_const_pointer(){return false;}; static bool is_const_value(){return true;}; static bool is_const_pointer_reference(){return false;} static bool is_const_pointer_pointer(){return false;} }; template<typename T> struct type_traits<T*>: public type_traits<T> { static bool is_reference(){return false;} static bool is_pointer(){return true;} static bool is_value(){return false;} static bool is_pointer_reference(){return false;} static bool is_pointer_pointer(){return false;} }; template<typename T> struct type_traits<const T*>: public type_traits<T*> { static bool is_const_reference(){return false;}; static bool is_const_pointer(){return true;}; static bool is_const_value(){return false;}; static bool is_const_pointer_reference(){return false;} static bool is_const_pointer_pointer(){return false;} }; template<typename T> struct type_traits<T&>: public type_traits<T> { static bool is_reference(){return true;} static bool is_pointer(){return false;} static bool is_value(){return false;} static bool is_pointer_reference(){return false;} static bool is_pointer_pointer(){return false;} }; template<typename T> struct type_traits<const T&>: public type_traits<T&> { static bool is_const_reference(){return true;}; static bool is_const_pointer(){return false;}; static bool is_const_value(){return false;}; static bool is_const_pointer_reference(){return false;} static bool is_const_pointer_pointer(){return false;} }; template<typename T> struct type_traits<T**>: public type_traits<T> { static bool is_reference(){return false;} static bool is_pointer(){return false;} static bool is_value(){return false;} static bool is_pointer_reference(){return false;} static bool is_pointer_pointer(){return true;} }; template<typename T> struct type_traits<const T**>: public type_traits<T**> { static bool is_const_reference(){return false;}; static bool is_const_pointer(){return false;}; static bool is_const_value(){return false;}; static bool is_const_pointer_reference(){return false;} static bool is_const_pointer_pointer(){return true;} }; template<typename T> struct type_traits<T*&>: public type_traits<T> { static bool is_reference(){return false;} static bool is_pointer(){return false;} static bool is_value(){return false;} static bool is_pointer_reference(){return true;} static bool is_pointer_pointer(){return false;} }; template<typename T> struct type_traits<const T*&>: public type_traits<T*&> { static bool is_const_reference(){return false;}; static bool is_const_pointer(){return false;}; static bool is_const_value(){return false;}; static bool is_const_pointer_reference(){return true;} static bool is_const_pointer_pointer(){return false;} }; template<typename T> struct type_expand_traits { typedef T value_type; typedef T& reference_type; typedef T* pointer_type; typedef const T const_value_type; typedef const T& const_reference_type; typedef const T* const_pointer_type; }; template<typename T> struct type_expand_traits<const T> { typedef T value_type; typedef T& reference_type; typedef T* pointer_type; typedef const T const_value_type; typedef const T& const_reference_type; typedef const T* const_pointer_type; }; template<typename T> struct type_expand_traits<T*> { typedef T* value_type; typedef T*& reference_type; typedef T** pointer_type; typedef const T* const_value_type; typedef const T*& const_reference_type; typedef const T** const_pointer_type; }; template<typename T> struct type_expand_traits<const T*> { typedef T* value_type; typedef T*& reference_type; typedef T** pointer_type; typedef const T* const_value_type; typedef const T*& const_reference_type; typedef const T** const_pointer_type; }; template<typename T> struct type_expand_traits<T**> { typedef T** value_type; typedef const T** const_value_type; }; template<typename T> struct type_expand_traits<const T**> { typedef T** value_type; typedef const T** const_value_type; }; template<typename T> struct type_expand_traits<T&> { typedef T value_type; typedef T& reference_type; typedef T* pointer_type; typedef const T const_value_type; typedef const T& const_reference_type; typedef const T* const_pointer_type; }; template<typename T> struct type_expand_traits<const T&> { typedef T value_type; typedef T& reference_type; typedef T* pointer_type; typedef const T const_value_type; typedef const T& const_reference_type; typedef const T* const_pointer_type; }; template<typename T> struct type_expand_traits<T*&> { typedef T* value_type; typedef T*& reference_type; typedef T** pointer_type; typedef const T* const_value_type; typedef const T*& const_reference_type; typedef const T** const_pointer_type; }; template<typename T> struct type_expand_traits<const T*&> { typedef T* value_type; typedef T*& reference_type; typedef T** pointer_type; typedef const T* const_value_type; typedef const T*& const_reference_type; typedef const T** const_pointer_type; }; NAMESPACE_TRAITS_END #endif
//pointer_integer_traits.hpp /* 函數地址與整數之間的轉換 */ #ifndef POINTER_INTEGER_TRAITS_INCLUDE #define POINTER_INTEGER_TRAITS_INCLUDE #include "traits_config.hpp" NAMESPACE_TRAITS_BEGIN // 指針與整數的轉換 template<typename funp> struct _pointer_integer_traits { protected: funp _fun; _pointer_integer_traits(unsigned int addr) { union { funp _fun; unsigned int _addr; }_u; _u._addr = addr; _fun = _u._fun; } public: operator funp() { return _fun; } }; template<> struct _pointer_integer_traits<unsigned int> { protected: unsigned int _addr; template<typename funp> _pointer_integer_traits(funp fun) { union { funp _fun; unsigned int _addr; }_u; _u._fun = fun; _addr = _u._addr; } public: operator unsigned int() { return _addr; } }; template<typename funp> struct pointer_integer_traits : public _pointer_integer_traits<funp> { pointer_integer_traits(const unsigned int addr):_pointer_integer_traits<funp>(addr){} }; template<typename funp> struct pointer_integer_traits<const funp*> : public _pointer_integer_traits<funp> { pointer_integer_traits(const unsigned int addr):_pointer_integer_traits<funp>(addr){} }; template<> struct pointer_integer_traits<int> : public _pointer_integer_traits<unsigned int> { template<typename funp> pointer_integer_traits(funp fun):_pointer_integer_traits<unsigned int>(fun){} template<typename funp> pointer_integer_traits(const funp* fun):_pointer_integer_traits<unsigned int>(fun){} }; template<> struct pointer_integer_traits<unsigned int> : public _pointer_integer_traits<unsigned int> { template<typename funp> pointer_integer_traits(funp fun):_pointer_integer_traits<unsigned int>(fun){} template<typename funp> pointer_integer_traits(const funp* fun):_pointer_integer_traits<unsigned int>(fun){} }; ////////////////////////////////////////// // 指針與引用的換 template<typename T> struct pointer_reference_traits; template<typename T> struct pointer_reference_traits<T&> { T* _p; pointer_reference_traits(const T* p) { _p = const_cast<T*>(p); } operator T&(){return *_p;} }; template<typename T> struct pointer_reference_traits<const T&> { T* _p; pointer_reference_traits(const T* p) { _p = const_cast<T*>(p); } operator const T&(){return *_p;} }; template<typename T> struct pointer_reference_traits<T*> { T* _p; pointer_reference_traits(const T& p) { _p = const_cast<T*>(&p); } operator T*(){return _p;} }; template<typename T> struct pointer_reference_traits<const T*> { T* _p; pointer_reference_traits(const T& p) { _p = const_cast<T*>(&p); } operator const T*(){return _p;} }; ///////////////////////////////// NAMESPACE_TRAITS_END #endif
// mfunction_traits.hpp #ifndef MFUNCTION_TRAITS_INCLUDE #define MFUNCTION_TRAITS_INCLUDE #include "traits_config.hpp" NAMESPACE_TRAITS_BEGIN template<typename Function> struct mfunction_traits_helper; template<typename Classtype,typename R> struct mfunction_traits_helper<R(Classtype::*)()> { enum{arity = 0}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg1)> { typedef Arg1 arg1; enum{arity = 1}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg2,Arg1)> { typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 2}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R,typename Arg3,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg3,Arg2,Arg1)> { typedef Arg3 arg3; typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 3}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R,typename Arg4,typename Arg3,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg4,Arg3,Arg2,Arg1)> { typedef Arg4 arg4; typedef Arg3 arg3; typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 4}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R,typename Arg5,typename Arg4,typename Arg3,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg5,Arg4,Arg3,Arg2,Arg1)> { typedef Arg5 arg5; typedef Arg4 arg4; typedef Arg3 arg3; typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 5}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R,typename Arg6,typename Arg5,typename Arg4,typename Arg3,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg6,Arg5,Arg4,Arg3,Arg2,Arg1)> { typedef Arg6 arg6; typedef Arg5 arg5; typedef Arg4 arg4; typedef Arg3 arg3; typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 6}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R,typename Arg7,typename Arg6,typename Arg5,typename Arg4,typename Arg3,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg7,Arg6,Arg5,Arg4,Arg3,Arg2,Arg1)> { typedef Arg7 arg7; typedef Arg6 arg6; typedef Arg5 arg5; typedef Arg4 arg4; typedef Arg3 arg3; typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 7}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R,typename Arg8,typename Arg7,typename Arg6,typename Arg5,typename Arg4,typename Arg3,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg8,Arg7,Arg6,Arg5,Arg4,Arg3,Arg2,Arg1)> { typedef Arg8 arg8; typedef Arg7 arg7; typedef Arg6 arg6; typedef Arg5 arg5; typedef Arg4 arg4; typedef Arg3 arg3; typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 8}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R, typename Arg9, typename Arg8,typename Arg7,typename Arg6,typename Arg5,typename Arg4,typename Arg3,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg9,Arg8,Arg7,Arg6,Arg5,Arg4,Arg3,Arg2,Arg1)> { typedef Arg9 arg9; typedef Arg8 arg8; typedef Arg7 arg7; typedef Arg6 arg6; typedef Arg5 arg5; typedef Arg4 arg4; typedef Arg3 arg3; typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 9}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R, typename Arg10, typename Arg9, typename Arg8,typename Arg7,typename Arg6,typename Arg5,typename Arg4,typename Arg3,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg10,Arg9,Arg8,Arg7,Arg6,Arg5,Arg4,Arg3,Arg2,Arg1)> { typedef Arg10 arg10; typedef Arg9 arg9; typedef Arg8 arg8; typedef Arg7 arg7; typedef Arg6 arg6; typedef Arg5 arg5; typedef Arg4 arg4; typedef Arg3 arg3; typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 10}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R, typename Arg11, typename Arg10, typename Arg9, typename Arg8,typename Arg7,typename Arg6,typename Arg5,typename Arg4,typename Arg3,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg11,Arg10,Arg9,Arg8,Arg7,Arg6,Arg5,Arg4,Arg3,Arg2,Arg1)> { typedef Arg11 arg11; typedef Arg10 arg10; typedef Arg9 arg9; typedef Arg8 arg8; typedef Arg7 arg7; typedef Arg6 arg6; typedef Arg5 arg5; typedef Arg4 arg4; typedef Arg3 arg3; typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 11}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R, typename Arg12, typename Arg11, typename Arg10, typename Arg9, typename Arg8,typename Arg7,typename Arg6,typename Arg5,typename Arg4,typename Arg3,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg12,Arg11,Arg10,Arg9,Arg8,Arg7,Arg6,Arg5,Arg4,Arg3,Arg2,Arg1)> { typedef Arg11 arg11; typedef Arg10 arg10; typedef Arg9 arg9; typedef Arg8 arg8; typedef Arg7 arg7; typedef Arg6 arg6; typedef Arg5 arg5; typedef Arg4 arg4; typedef Arg3 arg3; typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 12}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R, typename Arg13, typename Arg12, typename Arg11, typename Arg10, typename Arg9, typename Arg8,typename Arg7,typename Arg6,typename Arg5,typename Arg4,typename Arg3,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg13,Arg12,Arg11,Arg10,Arg9,Arg8,Arg7,Arg6,Arg5,Arg4,Arg3,Arg2,Arg1)> { typedef Arg13 arg13; typedef Arg11 arg11; typedef Arg10 arg10; typedef Arg9 arg9; typedef Arg8 arg8; typedef Arg7 arg7; typedef Arg6 arg6; typedef Arg5 arg5; typedef Arg4 arg4; typedef Arg3 arg3; typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 13}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R, typename Arg14, typename Arg13, typename Arg12, typename Arg11, typename Arg10, typename Arg9, typename Arg8,typename Arg7,typename Arg6,typename Arg5,typename Arg4,typename Arg3,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg14,Arg13,Arg12,Arg11,Arg10,Arg9,Arg8,Arg7,Arg6,Arg5,Arg4,Arg3,Arg2,Arg1)> { typedef Arg14 arg14; typedef Arg13 arg13; typedef Arg11 arg11; typedef Arg10 arg10; typedef Arg9 arg9; typedef Arg8 arg8; typedef Arg7 arg7; typedef Arg6 arg6; typedef Arg5 arg5; typedef Arg4 arg4; typedef Arg3 arg3; typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 14}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R, typename Arg15, typename Arg14, typename Arg13, typename Arg12, typename Arg11, typename Arg10, typename Arg9, typename Arg8,typename Arg7,typename Arg6,typename Arg5,typename Arg4,typename Arg3,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg15,Arg14,Arg13,Arg12,Arg11,Arg10,Arg9,Arg8,Arg7,Arg6,Arg5,Arg4,Arg3,Arg2,Arg1)> { typedef Arg15 arg15; typedef Arg14 arg14; typedef Arg13 arg13; typedef Arg11 arg11; typedef Arg10 arg10; typedef Arg9 arg9; typedef Arg8 arg8; typedef Arg7 arg7; typedef Arg6 arg6; typedef Arg5 arg5; typedef Arg4 arg4; typedef Arg3 arg3; typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 15}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R, typename Arg16, typename Arg15, typename Arg14, typename Arg13, typename Arg12, typename Arg11, typename Arg10, typename Arg9, typename Arg8,typename Arg7,typename Arg6,typename Arg5,typename Arg4,typename Arg3,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg16,Arg15,Arg14,Arg13,Arg12,Arg11,Arg10,Arg9,Arg8,Arg7,Arg6,Arg5,Arg4,Arg3,Arg2,Arg1)> { typedef Arg16 arg16; typedef Arg15 arg15; typedef Arg14 arg14; typedef Arg13 arg13; typedef Arg11 arg11; typedef Arg10 arg10; typedef Arg9 arg9; typedef Arg8 arg8; typedef Arg7 arg7; typedef Arg6 arg6; typedef Arg5 arg5; typedef Arg4 arg4; typedef Arg3 arg3; typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 16}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R, typename Arg17, typename Arg16, typename Arg15, typename Arg14, typename Arg13, typename Arg12, typename Arg11, typename Arg10, typename Arg9, typename Arg8,typename Arg7,typename Arg6,typename Arg5,typename Arg4,typename Arg3,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg17,Arg16,Arg15,Arg14,Arg13,Arg12,Arg11,Arg10,Arg9,Arg8,Arg7,Arg6,Arg5,Arg4,Arg3,Arg2,Arg1)> { typedef Arg17 arg17; typedef Arg16 arg16; typedef Arg15 arg15; typedef Arg14 arg14; typedef Arg13 arg13; typedef Arg11 arg11; typedef Arg10 arg10; typedef Arg9 arg9; typedef Arg8 arg8; typedef Arg7 arg7; typedef Arg6 arg6; typedef Arg5 arg5; typedef Arg4 arg4; typedef Arg3 arg3; typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 17}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R, typename Arg18, typename Arg17, typename Arg16, typename Arg15, typename Arg14, typename Arg13, typename Arg12, typename Arg11, typename Arg10, typename Arg9, typename Arg8,typename Arg7,typename Arg6,typename Arg5,typename Arg4,typename Arg3,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg18,Arg17,Arg16,Arg15,Arg14,Arg13,Arg12,Arg11,Arg10,Arg9,Arg8,Arg7,Arg6,Arg5,Arg4,Arg3,Arg2,Arg1)> { typedef Arg18 arg18; typedef Arg17 arg17; typedef Arg16 arg16; typedef Arg15 arg15; typedef Arg14 arg14; typedef Arg13 arg13; typedef Arg11 arg11; typedef Arg10 arg10; typedef Arg9 arg9; typedef Arg8 arg8; typedef Arg7 arg7; typedef Arg6 arg6; typedef Arg5 arg5; typedef Arg4 arg4; typedef Arg3 arg3; typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 18}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R, typename Arg19, typename Arg18, typename Arg17, typename Arg16, typename Arg15, typename Arg14, typename Arg13, typename Arg12, typename Arg11, typename Arg10, typename Arg9, typename Arg8,typename Arg7,typename Arg6,typename Arg5,typename Arg4,typename Arg3,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg19,Arg18,Arg17,Arg16,Arg15,Arg14,Arg13,Arg12,Arg11,Arg10,Arg9,Arg8,Arg7,Arg6,Arg5,Arg4,Arg3,Arg2,Arg1)> { typedef Arg19 arg19; typedef Arg18 arg18; typedef Arg17 arg17; typedef Arg16 arg16; typedef Arg15 arg15; typedef Arg14 arg14; typedef Arg13 arg13; typedef Arg11 arg11; typedef Arg10 arg10; typedef Arg9 arg9; typedef Arg8 arg8; typedef Arg7 arg7; typedef Arg6 arg6; typedef Arg5 arg5; typedef Arg4 arg4; typedef Arg3 arg3; typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 19}; typedef R result_type; typedef Classtype class_type; }; template<typename Classtype,typename R, typename Arg20, typename Arg19, typename Arg18, typename Arg17, typename Arg16, typename Arg15, typename Arg14, typename Arg13, typename Arg12, typename Arg11, typename Arg10, typename Arg9, typename Arg8,typename Arg7,typename Arg6,typename Arg5,typename Arg4,typename Arg3,typename Arg2,typename Arg1> struct mfunction_traits_helper<R(Classtype::*)(Arg20,Arg19,Arg18,Arg17,Arg16,Arg15,Arg14,Arg13,Arg12,Arg11,Arg10,Arg9,Arg8,Arg7,Arg6,Arg5,Arg4,Arg3,Arg2,Arg1)> { typedef Arg20 arg20; typedef Arg19 arg19; typedef Arg18 arg18; typedef Arg17 arg17; typedef Arg16 arg16; typedef Arg15 arg15; typedef Arg14 arg14; typedef Arg13 arg13; typedef Arg11 arg11; typedef Arg10 arg10; typedef Arg9 arg9; typedef Arg8 arg8; typedef Arg7 arg7; typedef Arg6 arg6; typedef Arg5 arg5; typedef Arg4 arg4; typedef Arg3 arg3; typedef Arg2 arg2; typedef Arg1 arg1; enum{arity = 20}; typedef R result_type; typedef Classtype class_type; }; template<typename _MFunctionP_Type> struct mfunction_traits : public mfunction_traits_helper<_MFunctionP_Type> { typedef _MFunctionP_Type MFunctionP_Type; }; NAMESPACE_TRAITS_END #endif
//mem_to_fun_converter.hpp #ifndef MEM_TO_FUN_CONVERTER_INCLUDE #define MEM_TO_FUN_CONVERTER_INCLUDE /*@brief: 換成員函數指針類型轉為普通函數類型*/ #include "traits_config.hpp" #include "mfunction_traits.hpp" NAMESPACE_TRAITS_BEGIN template<int Arity, typename funtype> struct mem_to_fun_converter_helper; template<typename funtype> struct mem_to_fun_converter_helper<0,funtype> { typedef mfunction_traits<funtype> _traits; typedef typename _traits::result_type(*FunctionP_Type)(); }; template<typename funtype> struct mem_to_fun_converter_helper<1,funtype> { typedef mfunction_traits<funtype> _traits; typedef typename _traits::result_type(*FunctionP_Type)(typename _traits::arg1); }; template<typename funtype> struct mem_to_fun_converter_helper<2,funtype> { typedef mfunction_traits<funtype> _traits; typedef typename _traits::result_type(*FunctionP_Type)(typename _traits::arg2,typename _traits::arg1); }; template<typename funtype> struct mem_to_fun_converter_helper<3,funtype> { typedef mfunction_traits<funtype> _traits; typedef typename _traits::result_type(*FunctionP_Type)(typename _traits::arg3,typename _traits::arg2,typename _traits::arg1); }; template<typename funtype> struct mem_to_fun_converter_helper<4,funtype> { typedef mfunction_traits<funtype> _traits; typedef typename _traits::result_type(*FunctionP_Type) ( typename _traits::arg4, typename _traits::arg3, typename _traits::arg2, typename _traits::arg1); }; template<typename funtype> struct mem_to_fun_converter_helper<5,funtype> { typedef mfunction_traits<funtype> _traits; typedef typename _traits::result_type(*FunctionP_Type) ( typename _traits::arg5, typename _traits::arg4, typename _traits::arg3, typename _traits::arg2, typename _traits::arg1); }; template<typename funtype> struct mem_to_fun_converter_helper<6,funtype> { typedef mfunct