Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(C++14) |
Defined in header <type_traits> | ||
template<class T> struct underlying_type; | (since C++11) | |
IfT
is a complete enumeration (enum) type, provides a member typedeftype
that names the underlying type ofT
.
Otherwise, the behavior is undefined. | (until C++20) |
Otherwise, if | (since C++20) |
If the program adds specializations forstd::underlying_type
, the behavior is undefined.
Contents |
Name | Definition |
type | the underlying type ofT |
template<class T> using underlying_type_t=typename underlying_type<T>::type; | (since C++14) | |
Eachenumeration type has anunderlying type, which can be
#include <iostream>#include <type_traits> enum e1{};enumclass e2{};enumclass e3:unsigned{};enumclass e4:int{}; int main(){constexprbool e1_t=std::is_same_v<std::underlying_type_t<e1>,int>;constexprbool e2_t=std::is_same_v<std::underlying_type_t<e2>,int>;constexprbool e3_t=std::is_same_v<std::underlying_type_t<e3>,int>;constexprbool 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';}
Possible output:
underlying type for 'e1' is non-intunderlying type for 'e2' is intunderlying type for 'e3' is non-intunderlying type for 'e4' is int
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 |
(C++11) | checks if a type is an enumeration type (class template)[edit] |
(C++23) | checks if a type is a scoped enumeration type (class template)[edit] |
(C++23) | converts an enumeration to its underlying type (function template)[edit] |