C++核心準則Enum.6:避免無名枚舉

C++核心準則Enum.6:避免無名枚舉

Enum.6: Avoid unnamed enumerations

Enum.6:避免無名枚舉

Reason(原因)

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

如果你無法為枚舉命名,說明這些值之間沒有關聯。

Example, bad(反面示例)

<code>enum { red = 0xFF0000, scale = 4, is_signed = 1 };/<code>

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:

使用常量表達式。例如:

<code>constexpr int red = 0xFF0000;
constexpr short scale = 4;
constexpr bool is_signed = true;/<code>

Enforcement(實施建議)

Flag unnamed enumerations.

提示無名枚舉。

原文鏈接:

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


覺得本文有幫助?請分享給更多人。

面向對象開發,面向對象思考!


分享到:


相關文章: