std:: underlying_type
From cppreference.net
C++
Metaprogramming library
| Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++14)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<type_traits>
|
||
|
template
<
class
T
>
struct underlying_type ; |
(C++11 起) | |
如果
T
是一个完整枚举(enum)类型,提供成员类型定义
type
来命名
T
的底层类型。
|
否则,行为未定义。 |
(C++20 前) |
|
否则,若
|
(C++20 起) |
如果程序为
std::underlying_type
添加特化,则行为未定义。
目录 |
成员类型
| 名称 | 定义 |
type
|
T
的基础类型
|
辅助类型
|
template
<
class
T
>
using underlying_type_t = typename underlying_type < T > :: type ; |
(自 C++14 起) | |
注释
每个 枚举类型 都有一个 底层类型 ,它可以是
- 显式指定(包括有作用域和无作用域枚举);
- 省略,此时有作用域枚举默认为 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++标准。
| 缺陷报告 | 适用范围 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 2396 | C++11 | 允许不完整的枚举类型 | 要求完整的枚举类型 |
参见
|
(C++11)
|
检查类型是否为枚举类型
(类模板) |
|
(C++23)
|
检查类型是否为有作用域枚举类型
(类模板) |
|
(C++23)
|
将枚举转换为其底层类型
(函数模板) |