std::conditional 的成员名称
时间: 2023-06-13 22:07:39 浏览: 64
`std::conditional` 模板类有两个公共成员类型:
1. `type`:一个类型,为 `std::conditional` 模板类的第一个模板参数 `bool` 的值为 `true` 时的类型,或第二个模板参数类型的值为 `false` 时的类型。
2. `value_type`:如果 `std::conditional` 模板类的第一个模板参数 `bool` 的值为 `true`,则 `value_type` 为第一个模板参数类型的值,否则 `value_type` 为第二个模板参数类型的值。
例如,以下代码演示了如何使用 `std::conditional` 模板类的 `type` 成员类型:
```cpp
#include <iostream>
#include <type_traits>
int main() {
// 如果 std::is_integral<int>::value 为 true,则 type 为 int,否则 type 为 double
std::conditional<std::is_integral<int>::value, int, double>::type type_variable;
std::cout << typeid(type_variable).name() << std::endl; // 输出 "i",表示 type_variable 的类型为 int
return 0;
}
```
在这个例子中,`std::conditional<std::is_integral<int>::value, int, double>::type` 表示如果 `std::is_integral<int>::value` 为 true,则 `type` 为 `int`,否则 `type` 为 `double`。最终,`type_variable` 的类型被确定为 `int`,因为 `std::is_integral<int>::value` 为 true。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)