Namespaces
Variants

std:: is_integral

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
(C++11)
(C++11)
is_integral
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
헤더 파일에 정의됨 <type_traits>
template < class T >
struct is_integral ;
(C++11부터)

std::is_integral UnaryTypeTrait 입니다.

T 정수형(integral type) 인지 확인합니다. 멤버 상수 value 를 제공하며, T bool , char , char8_t (C++20부터) , char16_t , char32_t , wchar_t , short , int , long , long long , 또는 구현에서 정의된 확장 정수형을 포함한 모든 signed, unsigned 및 cv-qualified 변형인 경우 value true 와 같습니다. 그렇지 않은 경우, value false 와 같습니다.

프로그램이 std::is_integral 또는 std::is_integral_v 에 대한 특수화를 추가하는 경우, 그 동작은 정의되지 않습니다.

목차

템플릿 매개변수

T - 확인할 타입

헬퍼 변수 템플릿

template < class T >
constexpr bool is_integral_v = is_integral < T > :: value ;
(C++17부터)

std:: integral_constant 로부터 상속됨

멤버 상수

value
[static]
true 만약 T 가 정수형 타입이면, false 그렇지 않으면
(public static member constant)

멤버 함수

operator bool
객체를 bool 로 변환, value 를 반환
(public member function)
operator()
(C++14)
value 를 반환
(public member function)

멤버 타입

타입 정의
value_type bool
type std:: integral_constant < bool , value >

가능한 구현

// 참고: 이 구현은 C++20 기능을 사용합니다
template<class T>
struct is_integral : std::bool_constant<
    requires (T t, T* p, void (*f)(T)) // T* 매개변수는 참조 타입을 제외합니다
    {
        reinterpret_cast<T>(t); // 클래스 타입을 제외합니다
        f(0); // 열거형 타입을 제외합니다
        p + t; // 아직 제외되지 않았지만 정수 타입이 아닌 모든 것을 제외합니다
    }> {};

예제

#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() {}

참고 항목

(C++20)
타입이 정수형 타입임을 명시
(concept)
[static]
정수 타입을 식별
( std::numeric_limits<T> 의 public static member constant)
타입이 부동소수점 타입인지 확인
(class template)
타입이 산술 타입인지 확인
(class template)
(C++11)
타입이 열거형 타입인지 확인
(class template)