std true_type false_type

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源码

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容