gpt4 book ai didi

c++ - 为什么 std::is_integral 将 bool 类型视为整数

转载 作者:行者123 更新时间:2023-12-04 11:29:11 24 4
gpt4 key购买 nike

在 C++ 类型特征中std::is_integral<T>::value即使 T 是 bool 也返回真,这根据其描述是正确的。
但是如果 bool 是与其他整数类型不同的类型,为什么在这种情况下将其视为整数类型?
为什么我们没有单独的 std::is_boolean 类型特征?

#include <iostream>
#include <type_traits>

int main()
{
std::cout << std::boolalpha;

std::cout << std::is_same<int, bool>::value << ' '; // ~ false
std::cout << std::is_same<unsigned int, bool>::value << ' '; // ~ false
std::cout << '\n';
std::cout << std::is_integral<bool>::value << ' '; // ~ true
return 0;
}

最佳答案

它是一个整数类型,因此它可以出现在整数常量表达式中。这在编写模板时非常重要 - truefalse通常用作非类型模板参数。

关于c++ - 为什么 std::is_integral 将 bool 类型视为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69218763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com