Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading