std::underlying_type
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <type_traits> 中定义
|
||
template< class T > struct underlying_type; |
(C++11 起) | |
定义一个成员typedef类型为基础类型的枚举
type
T
. 原文:
Defines a member typedef
type
of type that is the underlying type for the enumeration T
. [编辑] 会员类型
姓名
|
Definition |
type
|
T 的基础类型 |
[编辑] 注释
每个枚举类型都有一个基本的类型,它可以是
原文:
Each enumeration type has an underlying type, which can be
1。显式地指定(范围和无作用域的枚举)
原文:
1. Specified explicitly (both scoped and unscoped enumerations)
2。省略,在这种情况下,它是int范围的枚举或一个实现定义的整数类型能够代表所有的枚举值(无作用域的枚举)
原文:
2. Omitted, in which case it is int for scoped enumerations or an implementation-defined integral type capable of representing all values of the enum (for unscoped enumerations)
[编辑] 示例
#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'; }
输出:
underlying type for 'e1' is unsigned underlying type for 'e2' is int