四、条件类型

1 conditional_t

编译时的条件分支

参数

  • _Cond:是能够编译时得到结果的条件表达式
  • _Iftrue:如果 _Cond 为 true,::type为 _Iftrue
  • _Iffalse:如果 _Cond 为 true, ::type为 _Iffalse
  template<bool _Cond, typename _Iftrue, typename _Iffalse>            
    using conditional_t = typename conditional<_Cond, _Iftrue, _Iffalse>::type;

  template<bool _Cond, typename _Iftrue, typename _Iffalse>
    struct conditional
    { typedef _Iftrue type; };

  // Partial specialization for false.
  template<typename _Iftrue, typename _Iffalse>
    struct conditional<false, _Iftrue, _Iffalse>
    { typedef _Iffalse type; };

2 enable_if

条件成立,则正确执行。条件不成立,则,发生编译时错误。

可以根据条件的不同,控制函数的可见性。也就是说,条件成立,函数可以正确编译和调用,条件不成立,函数不可编译。
参数

  • _Cond:是能够编译时得到结果的条件表达式
  • _Tp :如果 _Cond 为 true,::type为 _Tp。如果 _Cond 为false, 就没有 ::type,会发生编译时错误。
  template<bool _Cond, typename _Tp = void>           
    using enable_if_t = typename enable_if<_Cond, _Tp>::type;
    
  template<bool, typename _Tp = void>
    struct enable_if
    { };

  // Partial specialization for true.
  template<typename _Tp>
    struct enable_if<true, _Tp>
    { typedef _Tp type; };

比如:optional类中,有这么一段代码

   template<typename... _Args>
	_GLIBCXX20_CONSTEXPR //修饰函数为constexpr属性
	enable_if_t<is_constructible_v<_Tp, _Args...>, _Tp&>
	emplace(_Args&&... __args)
	noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
	{
	  this->_M_reset();
	  this->_M_construct(std::forward<_Args>(__args)...);
	  return this->_M_get();
	}

enable_if_t<is_constructible_v<_Tp, _Args...>, _Tp&>约束 只有在 _Tp 类型可以由 _Args 参数构造时,该返回值类型才有效。 它不会直接影响函数的行为,但会影响函数是否可以被正确编译和调用。


多个类型的共同检查

分别对应于逻辑上的 &&、||和!

1 __and_

类似逻辑运算符 and, 对多个条件进行同时检查。如果全部满足,则返回true,否则,返回false

通过模板的递推展开,来对每一个类型进行检查。

  template<typename... _Bn>          
    inline constexpr bool __and_v = __and_<_Bn...>::value;

2 __or_

类似逻辑运算符 or, 对多个条件进行同时检查。如果有一个满足,则返回true,否则,返回false

__and_类似,也是模板递推展开

  template<typename... _Bn>  
    inline constexpr bool __or_v = __or_<_Bn...>::value;

3 __not_

类似逻辑运算符 not, 对单个条件进行检查。如果条件为false, 返回true, 否则,返回false

  template<typename _Pp>
    struct __not_
    : public __bool_constant<!bool(_Pp::value)>
    { };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值