頭一回發博客,來分享個有關C++類型萃取的編寫技巧

来源:http://www.cnblogs.com/openlib/archive/2016/03/31/5342816.html
-Advertisement-
Play Games

廢話不多說,上來貼代碼最實在,哈哈! 以下代碼量有點多,不過這都是在下一手一手敲出來的,小巧好用,把以下代碼複製出來,放到相應的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

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 源代碼:下載 VC開發程式單調的界面相信大家都是深有感觸,提到界面美化編程,人們都會說做界面不要用VC寫,太難了。一句俗語:難者不會,會者不難。VC的美化界面編程並沒有人們想像的那麼難。這篇文章是我寫的一個用戶登錄界面,但界面被我美化了,我將一步一步的來講解它的美化界面的實現步驟。相信有了這篇文章, ...
  • 規範需要平時編碼過程中註意,是一個慢慢養成的好習慣 1.基本原則 強制性原則: 1.字元串的拼加操作,必須使用StringBuilder; 2.try…catch的用法 try{ }catch{Exception e e.printStackTrace(); }finally{ }//在最外層的Ac ...
  • 如果是對話框程式直接在對話框的 初始化時,修改樣式 ...
  • *從”http:localhost:8080”說起 “http://localhost:8080”是一個url.url的組成如下麵部分: *當你在瀏覽器地址欄中輸入”http:www.cdtu.com”按下回車之後, 為什麼出現成都工業學院首頁? 你收到的網頁是從伺服器來的, 呈現在現在瀏覽器中, ... ...
  • 在C中使用指針的原因 避免副本 在函數調用的時候,可以只傳遞數據的引用,而不用傳遞數據 數據共用 兩段代碼可以同時操作同一份數據,而不是兩份獨立的副本 使用指針讀寫數據 船長,向東航行! ...
  • 文章出自:聽雲博客 題主將以三個章節的篇幅來講解JAVA IO的內容 。 第一節JAVA IO包的框架體系和源碼分析,第二節,序列化反序列化和IO的設計模塊,第三節非同步IO。 本文是第一節。 IO框架 從上圖我們可以看出IO可以分為兩大塊 位元組流和字元流 位元組流是 InputStream 和 Out ...
  • 自己項目中使用到了 結果在不同的windows 操作系統中,程式的運行不一致,在windows server 2008上可以很好的運行,但是到了windows7上去卡死了!!!!!!!!!!!!!!!!!!!!!! p.waitFor() 卡死了或者報錯: 如果改為: 則直接卡死了。如果將 p.wa ...
  • 歷屆試題 核桃的數量 時間限制:1.0s 記憶體限制:256.0MB 時間限制:1.0s 記憶體限制:256.0MB 問題描述 小張是軟體項目經理,他帶領3個開發組。工期緊,今天都在加班呢。為鼓舞士氣,小張打算給每個組發一袋核桃(據傳言能補腦)。他的要求是: 1. 各組的核桃數量必須相同 2. 各組內必 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...