C++核心准则Enum.6:避免无名枚举

面向对象思考

共 765字,需浏览 2分钟

 ·

2020-03-17 23:25

cdf87e2f83fd14014e3cf51d2b828fe7.webp

Enum.6: Avoid unnamed enumerations

Enum.6:避免无名枚举


Reason(原因)

If you can't name an enumeration, the values are not related

如果你无法为枚举命名,说明这些值之间没有关联。


Example, bad(反面示例)

enum { red = 0xFF0000, scale = 4, is_signed = 1 };

Such code is not uncommon in code written before there were convenient alternative ways of specifying integer constants.

在定义整形常量的便利的候选方式存在之前,这样的代码并不少见。


Alternative(可选项)

Use constexpr values instead. For example:

使用常量表达式。例如:

constexpr int red = 0xFF0000;
constexpr short scale = 4;
constexpr bool is_signed = true;

Enforcement(实施建议)

Flag unnamed enumerations.

提示无名枚举。


原文链接:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#enum6-avoid-unnamed-enumerations




觉得本文有帮助?请分享给更多人。

关注【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

浏览 27
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报