std::disjunction
时间: 2023-07-14 10:54:57 浏览: 77
`std::disjunction` 是 C++17 中引入的一个类型 trait(类型特性),用于判断一组布尔类型的条件是否至少有一个为真。它类似于逻辑运算中的逻辑或(OR)操作符。
`std::disjunction` 接受一个类型列表作为模板参数,并返回一个布尔类型(`std::true_type` 或 `std::false_type`)表示条件的结果。它会对列表中的每个条件进行求值,如果至少有一个条件为真,则返回 `std::true_type`,否则返回 `std::false_type`。
以下是 `std::disjunction` 的示例用法:
```cpp
#include <type_traits>
template<typename T>
void foo()
{
if constexpr (std::disjunction<std::is_same<T, int>, std::is_same<T, float>>::value)
{
// 当 T 是 int 或 float 时执行的代码
}
else
{
// 当 T 不是 int 也不是 float 时执行的代码
}
}
int main()
{
foo<int>(); // T 是 int,执行第一个代码块
foo<double>(); // T 不是 int 也不是 float,执行第二个代码块
}
```
在上面的示例中,`std::disjunction` 用于判断模板类型 `T` 是否是 `int` 或 `float`,并根据判断结果执行不同的代码块。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)