简体   繁体   中英

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. I'm expecting it to output "true", but instead it outputs "false":

#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.

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>;
                                                             //  ^^^^^^  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM