首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::underlying_type

Defined in header <type_traits>

template< class T > struct underlying_type;

(since C++11)

如果T是一个完整的枚举类型,提供一个成员类型type的基础类型。T...

否则,行为就没有定义。

成员类型

Name

Definition

type

the underlying type of T

帮助者类型

template< class T > using underlying_type_t = typename underlying_type<T>::type;

(since C++14)

注记

各枚举类型有一个底层类型也可以是。

  1. 显式指定%28范围和非作用域枚举%29。
  1. 省略,在这种情况下是int对于作用域枚举或实现定义的整数类型,该类型能够表示非作用域枚举%29的枚举%28的所有值。

缺陷报告

以下行为更改缺陷报告追溯应用于先前发布的C++标准。

DR

Applied to

Behavior as published

Correct behavior

LWG 2396

C++11

incomplete enumeration types were allowed

complete enumeration type required

二次

代码语言:javascript
复制
#include <iostream>
#include <type_traits>
 
enum e1 {};
enum class e2: int {};
 
int main() {
    bool e1_type = std::is_same<
        unsigned
       ,typename std::underlying_type<e1>::type
    >::value; 
 
    bool e2_type = std::is_same<
        int
       ,typename std::underlying_type<e2>::type
    >::value;
 
    std::cout
    << "underlying type for 'e1' is " << (e1_type?"unsigned":"non-unsigned") << '\n'
    << "underlying type for 'e2' is " << (e2_type?"int":"non-int") << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
underlying type for 'e1' is unsigned
underlying type for 'e2' is int

二次

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券