Boost – is_same

发布时间:2014-5-30 17:59
分类名称:Boost


struct integral_c_tag
{
    static const int value = 0;
};

//
常量基类

template< typename T, T N >
struct integral_c
{
    static const T value = N;        // XXX::value
等于 N
    typedef integral_c type;        // XXX::type
自身的 type
    typedef T value_type;            // XXX::value
的类型,一般可表示为:XXX::value_type var = XXX::value;
    typedef integral_c_tag tag;        //
目前未知 XXX::tag::value

    typedef integral_c< T, static_cast<T>((value + 1)) > next; //
类型为:next, next::value integral_c::value1;
    typedef integral_c< T, static_cast<T>((value - 1)) > prior; //
类型为:prior, prior::valueintegral_c::value1;
    operator T() const { return static_cast<T>(this->value); } //
重载运算符(), 返回value
};

//
静态变量
template< typename T, T N >
T const integral_c< T, N >::value;

//
原函数转发,将 T and val 转发给 integral_c
template <class T, T val>
struct integral_constant : public integral_c<T, val>
{
    typedef integral_constant<T,val> type;
};

//
原函数转发,将 bool and false 转发给 integral_constant
//
定义 TU模板,如果俩个类型不同的,将匹配这个模板,最终的父类integral_c::value等于false
template< typename T, typename U >
struct is_same :
    public integral_constant<bool, false>
{

};

//
偏特化
// U
被偏特化为T,如果俩个相同,则匹配到这个模板,最终的父类integral_c::value等于true
template< typename T >
struct is_same< T,T > :
    public integral_constant<bool, true>
{

};

int main(int argc, char* argv[])
{
    is_same<int, int*>::value_type bS;
    
    bS = is_same<int, int*>::value;
    bS = is_same<int, int>::value;

    return 0;
}