Namespaces
Variants

std:: underlying_type

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
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)

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

만약 T 가 완전한 열거형(enum) 타입인 경우, T 의 기반 타입을 명명하는 멤버 typedef type 를 제공합니다.

그렇지 않으면, 동작은 정의되지 않습니다.

(until C++20)

그렇지 않고, T 가 열거형 타입이 아닌 경우, type 멤버가 존재하지 않습니다. 그렇지 않은 경우 ( T 가 불완전한 열거형 타입), 프로그램은 ill-formed입니다.

(since C++20)

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

목차

멤버 타입

이름 정의
type T 의 기반 타입

헬퍼 타입

template < class T >
using underlying_type_t = typename underlying_type < T > :: type ;
(C++14부터)

참고 사항

열거형 타입 기본 타입 을 가지며, 이는 다음 중 하나일 수 있습니다

  1. 명시적으로 지정됨 (범위가 있는 열거형과 범위가 없는 열거형 모두);
  2. 생략된 경우, 이때 범위가 있는 열거형의 경우 int 이 되고, 범위가 없는 열거형의 경우 열거형의 모든 값을 표현할 수 있는 구현 정의 정수형이 됩니다.

예제

#include <iostream>
#include <type_traits>
enum e1 {};
enum class e2 {};
enum class e3 : unsigned {};
enum class e4 : int {};
int main()
{
    constexpr bool e1_t = std::is_same_v<std::underlying_type_t<e1>, int>;
    constexpr bool e2_t = std::is_same_v<std::underlying_type_t<e2>, int>;
    constexpr bool e3_t = std::is_same_v<std::underlying_type_t<e3>, int>;
    constexpr bool e4_t = std::is_same_v<std::underlying_type_t<e4>, int>;
    std::cout
        << "underlying type for 'e1' is " << (e1_t ? "int" : "non-int") << '\n'
        << "underlying type for 'e2' is " << (e2_t ? "int" : "non-int") << '\n'
        << "underlying type for 'e3' is " << (e3_t ? "int" : "non-int") << '\n'
        << "underlying type for 'e4' is " << (e4_t ? "int" : "non-int") << '\n';
}

가능한 출력:

underlying type for 'e1' is non-int
underlying type for 'e2' is int
underlying type for 'e3' is non-int
underlying type for 'e4' is int

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 2396 C++11 불완전한 열거형 타입이 허용됨 완전한 열거형 타입이 요구됨

참고 항목

(C++11)
타입이 열거형 타입인지 확인합니다
(클래스 템플릿)
타입이 범위 지정 열거형 타입인지 확인합니다
(클래스 템플릿)
열거형을 기반 타입으로 변환합니다
(함수 템플릿)