Warning C26812

The enum type 'type-name' is unscoped. Prefer 'enum class' over 'enum' (Enum.3)

Remarks

Prefer enum class over enum to prevent pollution in the global namespace.

Code analysis name: PreferScopedEnum

Example

The following example is from the C++ Core Guidelines:

void Print_color(int color);

enum Web_color { red = 0xFF0000, green = 0x00FF00, blue = 0x0000FF };
enum Product_info { Red = 0, Purple = 1, Blue = 2 };

Web_color webby = Web_color::blue;

// Clearly at least one of these calls is buggy.
Print_color(webby);               // C26812
Print_color(Product_info::Blue);  // C26812

These warnings are corrected by declaring the enum as enum class:

void Print_color(int color);

enum class Web_color { red = 0xFF0000, green = 0x00FF00, blue = 0x0000FF };
enum class Product_info { Red = 0, Purple = 1, Blue = 2 };

Web_color webby = Web_color::blue;
Print_color(webby);  // Error: cannot convert Web_color to int.
Print_color(Product_info::Red);  // Error: cannot convert Product_info to int.

See also

Enum.3 Prefer enum class over enum