Why am I allowed to use the incomplete enum class?

Why is the code below compiled without any errors?

enum class Enumeration;
void func()
{
    auto enumeration = static_cast<Enumeration>(2);
    auto value = static_cast<int>(enumeration);
}
+4
source share
1 answer

It compiles because the compiler knows at compile time the size Enumeration(which turns out to be empty).

You clearly see this syntax:

 enum class Enumeration : short;

The compiler knows everything there is to know about Enumeration. Enumerationis opaque-enum-declaration , which also means that type completei.e. you can use sizeofon it. If necessary, you can specify a list of counters in a later version (unless, of course, re-recording with a different base type is required).

, enum class, static_cast .

  • int, static_cast , .

enum afterall.

cppreference

, static_cast .

: enum int?

+5

Source: https://habr.com/ru/post/1681125/


All Articles