Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why std::is_integral considers bool type as integral

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;
}
like image 710
Ratnesh Tiwari Avatar asked Oct 15 '22 20:10

Ratnesh Tiwari


1 Answers

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.

like image 124
MSalters Avatar answered Oct 18 '22 13:10

MSalters