列挙型 'type-name' はスコープ外です。 'enum' より 'enum class' を優先します (Enum.3)
解説
グローバル名前空間の汚染を防ぐために、enum
よりもenum class
を優先します。
コード分析名: PreferScopedEnum
例
次の例は、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
これらの警告は、列挙型を列挙クラスとして宣言することで解決されます。
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.