std::underlying_type
From Get docs
Cpp/docs/latest/types/underlying type
std::underlying_type
Defined in header <type_traits>
|
||
---|---|---|
|
(since C++11) |
If T
is a complete enumeration type, provides a member typedef type
that names the underlying type of T
.
Otherwise, the behavior is undefined. | (until C++20) |
Otherwise, if T is not an enumeration type, there is no member type . Otherwise (T is an incomplete enumeration type), the program is ill-formed.
|
(since C++20) |
Member types
Name | Definition |
---|---|
type
|
the underlying type of T
|
Helper types
|
(since C++14) |
Notes
Each enumeration type has an underlying type, which can be.
1. Specified explicitly (both scoped and unscoped enumerations).
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).
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2396 | C++11 | incomplete enumeration types were allowed | complete enumeration type required |
Example
#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';
}
Output:
underlying type for 'e1' is unsigned
underlying type for 'e2' is int
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/types/underlying_type