namespace std {
template <class T>
struct is_integral;
template <class T>
inline constexpr bool is_integral_v = is_integral<T>::value; // C++17
}
概要
型T
が整数型かを調べる
効果
is_integral
は、型T
が整数型(cv修飾も許容される)であればtrue_type
から派生し、そうでなければfalse_type
から派生する。
以下のような型が、整数型として判定される:
bool
char
char16_t
char32_t
wchar_t
short
int
long
long long
enum
は整数型とは判定されない。
例
#include <type_traits>
static_assert(std::is_integral<int>::value == true, "value == true, int is integral");
static_assert(std::is_same<std::is_integral<int>::value_type, bool>::value, "value_type == bool");
static_assert(std::is_same<std::is_integral<int>::type, std::true_type>::value, "type == true_type");
static_assert(std::is_integral<int>() == true, "is_integral<int>() == true");
static_assert(std::is_integral<int*>::value == false, "value == false, int* is not integral");
static_assert(std::is_same<std::is_integral<int*>::value_type, bool>::value, "value_type == bool");
static_assert(std::is_same<std::is_integral<int*>::type, std::false_type>::value, "type == false_type");
static_assert(std::is_integral<int*>() == false, "is_integral<int*>() == false");
static_assert(std::is_integral<bool>::value == true, "bool is integral");
static_assert(std::is_integral<char>::value == true, "char is integral");
static_assert(std::is_integral<char32_t>::value == true, "char32_t is integral");
static_assert(std::is_integral<const long long>::value == true, "const long long is integral");
static_assert(std::is_integral<volatile unsigned>::value == true, "volatile unsigned is integral");
enum my_enum{};
static_assert(std::is_integral<my_enum>::value == false, "my_enum is not integral");
static_assert(std::is_integral<int&>::value == false, "int& is not integral");
static_assert(std::is_integral<int[1]>::value == false, "int[1] is not integral");
static_assert(std::is_integral<int ()>::value == false, "int () is not integral");
static_assert(std::is_integral<float>::value == false, "float is not integral");
int main(){}
27
#include <type_traits>
static_assert(std::is_integral<int>::value == true, "value == true, int is integral");
static_assert(std::is_same<std::is_integral<int>::value_type, bool>::value, "value_type == bool");
static_assert(std::is_same<std::is_integral<int>::type, std::true_type>::value, "type == true_type");
static_assert(std::is_integral<int>() == true, "is_integral<int>() == true");
static_assert(std::is_integral<int*>::value == false, "value == false, int* is not integral");
static_assert(std::is_same<std::is_integral<int*>::value_type, bool>::value, "value_type == bool");
static_assert(std::is_same<std::is_integral<int*>::type, std::false_type>::value, "type == false_type");
static_assert(std::is_integral<int*>() == false, "is_integral<int*>() == false");
static_assert(std::is_integral<bool>::value == true, "bool is integral");
static_assert(std::is_integral<char>::value == true, "char is integral");
static_assert(std::is_integral<char32_t>::value == true, "char32_t is integral");
static_assert(std::is_integral<const long long>::value == true, "const long long is integral");
static_assert(std::is_integral<volatile unsigned>::value == true, "volatile unsigned is integral");
出力
バージョン
言語
- C++11
処理系
- GCC: 4.3.4 ✅, 4.5.3 ✅, 4.6.1 ✅, 4.7.2 ✅
- Visual C++: 2008 (std::tr1) ✅, 2010 ✅, 2012 ✅, 2013 ✅, 2015 ✅
備考
上の例でコンパイラによってはエラーになる。GCC 4.3.4, 4.5.3, Visual C++ 2010 は integral_constant
が operator bool()
を持っていないためエラーになる。