C++类型萃取编写技巧(4)


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;
};

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/4b11dba9eca97ff5bf235e221cf6c9e1.html