class template
<type_traits>

std::underlying_type

template <class T> struct underlying_type;
Underlying type of enum
Obtains the underlying type of enum type T.

The underlying type is aliased as member type underlying_type::type.

The underlying type of an enum declared with enum class is int unless a different type is specified on declaration.

Notice that this class merely obtains a type using another type as model, but it does not transform values or objects between those types.

Template parameters

T
An enumeration type (enum).

Member types

member typedefinition
typeThe underlying type of enum type T.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// underlying_type example
#include <iostream>
#include <type_traits>

enum class A {a,b,c};
enum B : short {x,y,z};

int main() {

  typedef std::underlying_type<A>::type A_under;   // int
  typedef std::underlying_type<B>::type B_under;   // short

  std::cout << std::boolalpha;
  std::cout << "typedefs of int:" << std::endl;

  std::cout << "A_under: " << std::is_same<int,A_under>::value << std::endl;
  std::cout << "B_under: " << std::is_same<int,B_under>::value << std::endl;

  return 0;
}


Output:
typedefs of int:
A_under: true
B_under: false

See also

<type_traits>