STL之四 type traits

来源:互联网 发布:男士护肤面膜 知乎 编辑:程序博客网 时间:2024/06/05 05:28

<type_traits>

从C++11起,提供了很多类型判断的接口,详细见链接 http://en.cppreference.com/w/cpp/types。

统一的对外接口(调用方式):is_xxx<value_type>::value 

如果value_type满足xxx的特性,则该式返回true;否则返回false。

以 is_trivially_destructible 为例进行详细讲解。is_trivially_destructible<value_type>提供的标准对外接口有value, value_type, type, bool类型转换函数(把is_trivially_destructible<value_type>转换成bool类型的值); 如果value_type为trivially destructible的(比如为scalar类型或者没有显示定义析构函数且没有继承的复杂类型等等),则该式返回true,否则返回false。C++规定,is_xx要继承具现化的integral_constant。integral_constant标准接口要提供value, value_type, type, 类型转换函数,与()运算符重载函数(c++14)。见http://en.cppreference.com/w/cpp/types/integral_constant,下面是一个可能的实现

template<class T, T v>struct integral_constant {    static constexpr T value = v;    typedef T value_type;    typedef integral_constant type;    constexpr operator value_type() const noexcept { return value; } // (*) c++11    constexpr value_type operator()() const noexcept { return value; } // c++14};
integral_constant<Value_type, Value>::type 就是 Value_type

integral_constant<Value_type, Value>::value 就是 Value

第(*)行是一个类型转换操作符函数,就是把integral_constant<Value_type, Value>转换成Value_type,且赋值Value。详细见http://www.cnblogs.com/edwardlost/archive/2010/12/01/1887983.html

C++标准中,true_type和false_type的定义为

typedef integral_constant<bool, true> true_type;

typedef integral_constant<bool, false> false_type;

讲完 integral_constant之后,接着讲is_trivially_destructible。假设编译器有提供底层的函数 __has_trivial_destructor(Value_type),那么可能的实现会是

template<class T>struct is_tivially_destructible : public integral_constant<bool, __has_trivial_destructor(T)>{};
也就是说,当T has trivial destructor 的时候,is_trivially_destructible<T>::value 返回true;否则返回false。

gcc4.9.1中,其实现如下(https://gcc.gnu.org/onlinedocs/gcc-4.9.1/libstdc++/api/a01562_source.html)

// Meta programming helper types.template<bool, typename, typename>struct conditional;   template<typename _B1, typename _B2>struct __and_<_B1, _B2>: public conditional<_B1::value, _B2, _B1>::type { };  template<typename _Tp>struct is_trivially_destructible : public __and_<is_destructible<_Tp>, integral_constant<bool, __has_trivial_destructor(_Tp)> >::type{ };
gcc中进行了两个判断 is_destructible 和 __has_trivial_destructor。其中is_destructible的实现有点复杂,conditional元编程也有待解决。

遗留问题1:conditional 元编程

遗留问题2:is_destructible()的详细实现


附加疑问:C++标准规定 is_trivially_destructible<value_type>必须提供一个bool类型转换函数,把 is_trivially_destructible<value_type>转换为bool类型;但是如果按照下面自己的这种实现,是否会提供这个接口呢?

template<class T>struct is_tivially_destructible : public integral_constant<bool, __has_trivial_destructor(T)>{};
回答:这种实现是可以提供这个接口的。因为integral_constant<bool, __has_trivial_destructor(T)>中有一个 bool 类型转换函数,把integral_constant<bool, __has_trivial_destructor(T)>转换成bool类型,而is_trivially_destructible<T> 又继承了 integral_constant<bool, __has_trivial_destructor(T)>,那么is_trivially_destructible<T>中就会自动有一个bool转换函数把 is_trivially_destructible<T> 转换为 bool类型,返回值就是 integral_constant<bool, _has_trivial_destructor(T)>中的bool转换函数的返回值。

验证

struct A{  operator int(){return 1;}  };struct B : public A{    };void f(int i){ cout << i  << endl; }int main() {   B b;   f(b);}
输出 :1




0 0