In C++ type traits
std::is_integral<T>::value
returns true even if T is bool which is correct as per its description.
But if bool is a different type than other integral types, why its considered as integral type in this case? why we don't have a separate std::is_boolean type trait?
#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;
}
It's an integral type so it can appear in Integral Constant Expressions. This is quite important when it comes to writing templates - true
and false
are commonly used as non-type template parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With