前置依赖:
- integral_constant
- conditional
源码解读:
template<typename...>
struct __and_;
template<>
struct __and_<>
: public true_type
{ };
template<typename _B1>
struct __and_<_B1>
: public _B1
{ };
template<typename _B1, typename _B2>
struct __and_<_B1, _B2>
: public conditional<_B1::value, _B2, _B1>::type
{ };
template<typename _B1, typename _B2, typename _B3, typename... _Bn>
struct __and_<_B1, _B2, _B3, _Bn...>
: public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type
{ };
- 此部分和 _or 模板类大差不差, 但是有一点细微差别
- 在接受两个参数的时候, 使用 conditional 模板类, 但是将 _Iftrue 和 _Iffalse 的实参交换了, 也就是
conditional<_B1::value, _B2, _B1>::type 这是因为 and 逻辑应该是遇非即非, 也就是说, 如果 _B1 为 false_type 的时候, 取出的 value 值应该为 false, 这时候 conditional 选择 _Iffalse 的参数, 由于短路规则 _B1 && _B2 应该直接取 _B1 (false , 因此 _B1 放在 _Iffalse 的位置上.
- 多参数的行为是一样的, 这就是 _and 和 _or 的细微差别
- SFINAE 技术, 返回结果同样为 true_type 或者 false_type