簡體   English   中英

std::is_same_v<> 為使用類型聲明的枚舉返回 false?

[英]std::is_same_v<> returns false for enum declared with type?

下面是我按照 std::underlying_type示例測試的一些代碼。 我期待它 output “真”,但它輸出“假”:

#include <iostream>
#include <type_traits>

enum class Color: int
{
  Red,
  Blue
};

int main()
{
  constexpr bool match = std::is_same_v<std::underlying_type<Color>, int>;

  std::cout << std::boolalpha << match << std::endl;
}

我錯過了什么明顯的東西嗎?

std::underlying_type是一個 class ,其成員類型別名type對實際的底層類型進行編碼。

如果你想要那種類型,你需要明確地要求它:

constexpr bool match = std::is_same_v<std::underlying_type_t<Color>, int>;
                                                      //  ^^  

或者

constexpr bool match = std::is_same_v<std::underlying_type<Color>::type, int>;
                                                             //  ^^^^^^  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM