Boost - enable_if

发布时间:2011-5-27 10:36
分类名称:Boost


// ============ enable_if ==============

// 作用,是否将函数踢出重载候选列表。

template <bool B, class T = void>

struct enable_if_c {

typedef T type;

};

 

template <class T>

struct enable_if_c<false, T> {};

 

template <class Cond, class T = void>

struct enable_if : public enable_if_c<Cond::value, T> {};

 

template <bool B, class T>

struct lazy_enable_if_c {

typedef typename T::type type;

};

 

template <class T>

struct lazy_enable_if_c<false, T> {};

 

template <class Cond, class T>

struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {};

 

 

template <bool B, class T = void>

struct disable_if_c {

typedef T type;

};

 

template <class T>

struct disable_if_c<true, T> {};

 

template <class Cond, class T = void>

struct disable_if : public disable_if_c<Cond::value, T> {};

 

template <bool B, class T>

struct lazy_disable_if_c {

typedef typename T::type type;

};

 

template <class T>

struct lazy_disable_if_c<true, T> {};

 

template <class Cond, class T>

struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {};

 

 

// ============ ice_not ==============

// 作用:取反

// 如果传入的bool值为true,则value值为false

// 如果传入的bool值为false,则value值为false

 

template <bool b>

struct ice_not

{

    static const bool value = true;

};

 

template <>

struct ice_not<true>

{

    static const bool value = false;

};

 

ice_not<true>::value;  // value = false;

ice_not<false>::value; // value = true;

 

// ============ bool_ ==============

// 作用:存储 bool valuetypevalue_type

template< bool C_ > struct bool_

{

    static const bool value = C_;

    typedef integral_c_tag tag;

    typedef bool_ type;

    typedef bool value_type;

    operator bool() const { return this->value; }

};

template< bool C_ >

bool const bool_<C_>::value;

 

template< bool C_ > struct bool_;

typedef bool_<true> true_;

typedef bool_<false> false_;

 

// ============ integral_c ==============

// 作用:所有整形的父类

struct integral_c

{

    static const T value = N;

    typedef integral_c type;

    typedef T value_type;

    typedef integral_c_tag tag;

    typedef integral_c< T, static_cast<T>((value + 1)) > next;

    typedef integral_c< T, static_cast<T>((value - 1)) > prior;

    operator T() const { return static_cast<T>(this->value); }

};

template< typename T, T N >

T const integral_c< T, N >::value;

}

 

template< bool C >

struct integral_c<bool, C>

{

    static const bool value = C;

    typedef integral_c_tag tag;

    typedef integral_c type;

    typedef bool value_type;

    operator bool() const { return this->value; }

};

 

template <class T, T val>

struct integral_constant : public integral_c<T, val>

{

   typedef integral_constant<T,val> type;

};

template<> struct integral_constant<bool,true> : public mpl::true_

{

   typedef integral_constant<bool,true> type;

};

template<> struct integral_constant<bool,false> : public mpl::false_

{

   typedef integral_constant<bool,false> type;

};

 

typedef integral_constant<bool,true> true_type;

typedef integral_constant<bool,false> false_type;

 

 

#define BOOST_TT_AUX_BOOL_C_BASE(C) : ::boost::integral_constant<bool,C>

 

#define BOOST_TT_AUX_BOOL_TRAIT_SPEC1(trait,sp,C) \

template<> struct trait< sp > \

    BOOST_TT_AUX_BOOL_C_BASE(C) \

{ \

}; \

 

#   define BOOST_TT_AUX_BOOL_TRAIT_CV_SPEC1(trait,sp,value) \

    BOOST_TT_AUX_BOOL_TRAIT_SPEC1(trait,sp,value) \

    BOOST_TT_AUX_BOOL_TRAIT_SPEC1(trait,sp const,value) \

    BOOST_TT_AUX_BOOL_TRAIT_SPEC1(trait,sp volatile,value) \

    BOOST_TT_AUX_BOOL_TRAIT_SPEC1(trait,sp const volatile,value) \

 

// ============== is_integral ================

// 作用:is_integral<T>::value 判断T是否为整数类型,如果是,则value为值为true;反之,valuefalse

BOOST_TT_AUX_BOOL_TRAIT_DEF1(is_integral,T,false)

 

BOOST_TT_AUX_BOOL_TRAIT_CV_SPEC1(is_integral,unsigned char,true)

BOOST_TT_AUX_BOOL_TRAIT_CV_SPEC1(is_integral,unsigned short,true)

BOOST_TT_AUX_BOOL_TRAIT_CV_SPEC1(is_integral,unsigned int,true)

BOOST_TT_AUX_BOOL_TRAIT_CV_SPEC1(is_integral,unsigned long,true)

 

BOOST_TT_AUX_BOOL_TRAIT_CV_SPEC1(is_integral,signed char,true)

BOOST_TT_AUX_BOOL_TRAIT_CV_SPEC1(is_integral,signed short,true)

BOOST_TT_AUX_BOOL_TRAIT_CV_SPEC1(is_integral,signed int,true)

BOOST_TT_AUX_BOOL_TRAIT_CV_SPEC1(is_integral,signed long,true)

 

BOOST_TT_AUX_BOOL_TRAIT_CV_SPEC1(is_integral,bool,true)

BOOST_TT_AUX_BOOL_TRAIT_CV_SPEC1(is_integral,char,true)

 

// 如果Functor不是整形,is_integral<Functor>::value false

// ic_not<false>::value true

// enable_if_c<true, int>::type 正确。

// 带此参数的的函数被选入重载候选集。

// 反之,如果Functor是个整形,则函数被踢出。

typename enable_if_c

         <

            ice_not

            <

                is_integral<Functor>::value

            >::value, int

         >::type;

        

// 我们希望的是,传入的参数不是整数类型,此语句通过一个整形的Type(类型),

// 最后得出此Type是否是整形的一个过程。(Type -> true, false).

// 实现过程:

// T不为整形类型时,is_integral继承于integral_constant<bool,false>

// T为整形类型时,is_integral继承于integral_constant<bool,true>

// 看来true, false是写死在代码里面的。