☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

不应使用匿名枚举声明

5.2.3 ID_forbidUnnamedEnum
目录 › next › previous

匿名枚举声明相当于在当前作用域定义常量,但类型不够明确。

示例:

enum { rabbit = 0xAA, carrot = 1234 };  // Non-compliant

如果无法确定枚举类型的名称,也意味着各枚举项不应聚为一类。

应改为:

const int rabbit = 0xAA;  // Compliant
const int carrot = 1234;  // Compliant

参考

C++ Core Guidelines Enum.6
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.