简体   繁体   English

std::is_same_v<> 为使用类型声明的枚举返回 false?

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

Below is some code I am testing following the example for std::underlying_type.下面是我按照 std::underlying_type示例测试的一些代码。 I'm expecting it to output "true", but instead it outputs "false":我期待它 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;
}

Have I missed something obvious?我错过了什么明显的东西吗?

std::underlying_type is a class with a member type alias type which encodes the actual underlying type. std::underlying_type是一个 class ,其成员类型别名type对实际的底层类型进行编码。

If you want that type, you need to ask for it explicitly:如果你想要那种类型,你需要明确地要求它:

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

or或者

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