types_is_integral (정수 타입 판별)
이 페이지에서는 C++ 표준 라이브러리의 std::is_integral 타입 특성에 대해 설명해요. std::is_integral은 주어진 타입 T가 정수 타입인지 여부를 컴파일 타임에 판별하는 데 사용해요. 이 특성은 템플릿 메타프로그래밍에서 타입 검사에 널리 활용돼요.
출처: cppreference
본문
std::is_integral은 UnaryTypeTrait이에요. T가 정수 타입인지 확인해요. T가 bool, char, char8_t(C++20 이후), char16_t, char32_t, wchar_t, short, int, long, long long 또는 구현 정의 확장 정수 타입(부호 있는/부호 없는/cv 한정 변형 포함)이라면 value는 true가 돼요. 그 외에는 value가 false예요. 프로그램에서 std::is_integral이나 std::is_integral_v에 전문화를 추가하면 동작이 정의되지 않아요.
템플릿 매개변수
헬퍼 변수 템플릿
template < class T > constexpr bool is_integral_v = is_integral < T >:: value; |
(C++17 이후) |
std::integral_constant에서 상속받은 멤버
멤버 상수
value [static] |
T가 정수 타입이면 true, 아니면 false (공용 정적 멤버 상수) |
멤버 함수
operator bool |
객체를 bool로 변환하고 value를 반환해요 (공용 멤버 함수) |
operator() (C++14) |
value를 반환해요 (공용 멤버 함수) |
멤버 타입
| 타입 |
정의 |
value_type |
bool |
type |
std :: integral_constant < bool, value > |
가능한 구현
// Note: this implementation uses C++20 facilities
template < class T >
struct is_integral : std::bool_constant <
requires ( T t, T * p, void ( * f )( T )) // T* parameter excludes reference types
{
reinterpret_cast < T > ( t ); // Exclude class types
f ( 0 ); // Exclude enumeration types
p + t ; // Exclude everything not yet excluded but integral types
}
> {};
예제
#include <type_traits>
static_assert
(
std::is_integral_v<float> == false &&
std::is_integral_v<int*> == false &&
std::is_integral_v<int> == true &&
std::is_integral_v<const int> == true &&
std::is_integral_v<bool> == true &&
std::is_integral_v<char> == true
);
class A {};
static_assert(std::is_integral_v<A> == false);
struct B { int x:4; };
static_assert(std::is_integral_v<B> == false);
using BF = decltype(B::x); // bit-field's type
static_assert(std::is_integral_v<BF> == true);
enum E : int {};
static_assert(std::is_integral_v<E> == false);
template <class T>
constexpr T same(T i)
{
static_assert(std::is_integral<T>::value, "Integral required.");
return i;
}
static_assert(same('"') == 042);
int main() {}
같이 보기
integral (C++20) |
타입이 정수 타입임을 명시해요 (concept) [edit] |
is_integer [static] |
정수 타입을 식별해요 (std::numeric_limits<T>의 공용 정적 멤버 상수) [edit] |
is_floating_point (C++11) |
타입이 부동소수점 타입인지 확인해요 (클래스 템플릿) [edit] |
is_arithmetic (C++11) |
타입이 산술 타입인지 확인해요 (클래스 템플릿) [edit] |
is_enum (C++11) |
타입이 열거형 타입인지 확인해요 (클래스 템플릿) [edit] |
is_integral_type (C++26) |
반영된 타입이 정수 타입인지 확인해요 (함수) [edit] |
더 알아보기 (Learn more)
cppreference