头一回发博客,来分享个有关C++类型萃取的编写技巧
Posted xiaojack
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了头一回发博客,来分享个有关C++类型萃取的编写技巧相关的知识,希望对你有一定的参考价值。
废话不多说,上来贴代码最实在,哈哈!
以下代码量有点多,不过这都是在下一手一手敲出来的,小巧好用,把以下代码复制出来,放到相应的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 mfunction_traits<funtype> _traits; typedef typename _traits::result_type(*FunctionP_Type) ( typename _traits::arg6, typename _traits::arg5, typename _traits::arg4, typename _traits::arg3, typename _traits::arg2, typename _traits::arg1); }; template<typename funtype> struct mem_to_fun_converter_helper<7,funtype> { typedef mfunction_traits<funtype> _traits; typedef typename _traits::result_type(*FunctionP_Type) ( typename _traits::arg7, typename _traits::arg6, typename _traits::arg5, typename _traits::arg4, typename _traits::arg3, typename _traits::arg2, typename _traits::arg1); }; template<typename以上是关于头一回发博客,来分享个有关C++类型萃取的编写技巧的主要内容,如果未能解决你的问题,请参考以下文章