<type_traits>

類模板
<type_traits>

std::underlying_type

template <class T> struct underlying_type;
列舉型別的底層型別
獲取列舉型別的底層型別T.

底層型別被別名為成員型別underlying_type::type.

宣告為enum class的 C++ 等效檔案是int除非宣告中指定了不同的型別。

請注意,此類僅使用另一種型別作為模型來獲取型別,但它不會在這些型別之間轉換值或物件。

模板引數

T
列舉型別(enum).

成員型別

成員型別定義
型別列舉型別的底層型別T.

示例

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;
}

輸出
typedefs of int:
A_under: true
B_under: false


另見