[英]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.