1、源码
实现方式
定义别名。
性能开销
为常量表达式,不涉及运行时开销。
调用stl内容
integral_constant
true_type
true_type为integral_constant<bool, true>的别名。
/// The type used as a compile-time boolean with true value.
typedef integral_constant<bool, true> true_type;
false_type
false_type为integral_constant<bool, true>的别名。
/// The type used as a compile-time boolean with false value.
typedef integral_constant<bool, false> false_type;
2、代码示例
使用is_void<_Tp>>::value判断获取bool值。
template<typename>
struct __is_void_helper
: public false_type { };
template<>
struct __is_void_helper<void>
: public true_type { };
/// is_void
template<typename _Tp>
struct is_void
: public __is_void_helper<typename remove_cv<_Tp>::type>::type
{ };
/// add_pointer
template<typename _Tp, bool = __or_<__is_referenceable<_Tp>,
is_void<_Tp>>::value>
struct __add_pointer_helper
{ typedef _Tp type; };
3、应用场景
作为一些辅助模版的基类,提供一个默认值。
参考:gcc8源码