【C++基础知识】C++类型特征组合:`disjunction_v` 和 `conjunction_v` 深度解析

这两个模板是C++17引入的类型特征组合工具,用于构建更复杂的类型判断逻辑。下面我将从技术实现到实际应用进行全面剖析:

一、基本概念与C++引入版本

1. std::disjunction_v (逻辑OR)

  • 引入版本:C++17
  • 功能:对多个类型特征进行逻辑或运算
  • 等价形式T1::value || T2::value || ... || Tn::value
  • 别名模板disjunction_v<T...>disjunction<T...>::value 的简写

2. std::conjunction_v (逻辑AND)

  • 引入版本:C++17
  • 功能:对多个类型特征进行逻辑与运算
  • 等价形式T1::value && T2::value && ... && Tn::value
  • 别名模板conjunction_v<T...>conjunction<T...>::value 的简写

二、底层实现原理

1. disjunction 的可能实现

template<class...> struct disjunction : std::false_type {
   
   };
template<class B1> struct disjunction<B1> : B1 {
   
   };
template<class B1, class... Bn>
struct disjunction<B1, Bn...> : 
    std::conditional_t<B1::value, B1, disjunction<Bn...>> {
   
   };

2. conjunction 的可能实现

template<class...> struct conjunction 
EnergySankeyWidget.cpp:182:54: No matching member function for call to 'arg' qstring.h:317:27: candidate template ignored: requirement 'std::conjunction_v<std::disjunction<std::is_convertible<int (*)(double), unsigned long long>, std::is_convertible<int (*)(double), long long>>, std::negation<std::disjunction<std::is_same<int (*)(double), qfloat16>, std::is_floating_point<int (*)(double)>>>, std::negation<std::conjunction<std::negation<QtPrivate::treat_as_integral_arg<int (*)(double)>>, std::is_convertible<int (*)(double), QAnyStringView>>>>' was not satisfied [with T = int (*)(double)] qstring.h:332:27: candidate template ignored: requirement 'disjunction<std::is_same<int (*)(double), qfloat16>, std::is_floating_point<int (*)(double)>>::value' was not satisfied [with T = int (*)(double)] qstring.h:337:27: candidate function template not viable: requires at most 3 arguments, but 4 were provided qstring.h:357:5: candidate template ignored: requirement 'std::conjunction_v<std::conjunction<std::negation<QtPrivate::treat_as_integral_arg<int (&)(double)>>, std::is_convertible<int (&)(double), QAnyStringView>>, std::conjunction<std::negation<QtPrivate::treat_as_integral_arg<int>>, std::is_convertible<int, QAnyStringView>>, std::conjunction<std::negation<QtPrivate::treat_as_integral_arg<char>>, std::is_convertible<char, QAnyStringView>>, std::conjunction<std::negation<QtPrivate::treat_as_integral_arg<int>>, std::is_convertible<int, QAnyStringView>>>' was not satisfied [with Args = <int (&)(double), int, char, int>]
最新发布
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沐怡旸--指针诗笺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值