-1

At the entry of reinterpret_cast, cppref says:

An expression of integral, enumeration, pointer, or pointer-to-member type can be converted to its own type.The resulting value is the same as the value of expression. (since C++11)

However, the following code cannot be compiled (clang 5.0 with -std=c++1z):

enum class A : int {};

int main()
{
    A a{ 0 };
    reinterpret_cast<int>(a); // error : reinterpret_cast from 'A' to 'int' is not allowed
}

Why does reinterpret_cast not behave as the C++ standard says?

xmllmx
  • 37,882
  • 21
  • 139
  • 300

2 Answers2

5

The type of a is A, not int. The syntax enum class A : int makes int the underlying type of A, which is a special relationship, but not an "is-a" relationship.

(static_cast will perform this conversion.)

Potatoswatter
  • 131,100
  • 23
  • 249
  • 407
3

int is the "underlying type", but the enumeration itself is a separate type.

From [dcl.enum]/5:

Each enumeration defines a type that is different from all other types. Each enumeration also has an underlying type. The underlying type can be explicitly specified using an enum-base.

Jerry Coffin
  • 455,417
  • 76
  • 598
  • 1,067