c++enumsc++11

Anonymous enum classes


Is is possible to write a anonymous enum class and then comparing what it contains? Eg.

enum class { APPLE, BANANA } fruitType;
// ...
if (fruitType == fruitType::APPLE)
    // ...

Solution

  • No, fruitType is a variable (despite Type in the name). You cannot use a variable name to access things about its type.

    The idea with enum class is that the values are not visible outside the definition unless you prefix them with the type name. If the type doesn't have a name, this will be difficult!