Search code examples
c++enumstype-traitsstd-byte

std::is_enum_v<std::byte> evaluates to true


According to the standard reference, std::is_enum_v evaluates to true for enumeration types and to false otherwise. An enumeration type starts with an enum key, i.e. either one of enum, enum class or enum struct.

In wrote the following testprogram:

#include <type_traits>
#include <iostream>

using to_examine = std::byte;

enum class Foo {
    ONE,
    TWO 
};

int main() {
    if constexpr (std::is_enum_v<std::byte>) {
        std::cout << "byte counts as enum" << std::endl;
    }   

    if constexpr(std::is_enum_v<int>) {
        std::cout << "int counts as enum" << std::endl;
    }   

    if constexpr(std::is_enum_v<Foo>) {
        std::cout << "scoped enum counts as enum" << std::endl;
    }   
}

which prints out the following:

byte counts as enum
scoped enum counts as enum

I am working with clang15 and g++12. Is this a bug or a feature?


Solution

  • std::byte is defined by the standard as an enumeration type.

    See std::byte documentation:

    enum class byte : unsigned char {};   // (since C++17) 
    

    If you are interested in the reason it is defined like this, there's some info here: