C++核心準則Enum.7: 只在必要時定義枚舉的底層類型

C++核心準則Enum.7:  只在必要時定義枚舉的底層類型

Enum.7: Specify the underlying type of an enumeration only when necessary

Enum.7: 只在必要時定義枚舉的底層類型

Reason(原因)

The default is the easiest to read and write. int is the default integer type. int is compatible with C enums.

默認的類型更容易讀寫。int是默認的整數類型。int和C語言枚舉類型兼容。

Example(示例)

<code>enum class Direction : char { n, s, e, w,
ne, nw, se, sw }; // underlying type saves space

enum class Web_color : int32_t { red = 0xFF0000,
green = 0x00FF00,
blue = 0x0000FF }; // underlying type is redundant/<code>

Note(注意)

Specifying the underlying type is necessary in forward declarations of enumerations:

在前置聲明枚舉時需要定義枚舉的底層類型。

<code>enum Flags : char;

void f(Flags);

// ....

enum flags : char { /* ... */ };/<code>

Enforcement(實施建議)

????

原文鏈接:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#enum7-specify-the-underlying-type-of-an-enumeration-only-when-necessary


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

關注【面向對象思考】輕鬆學習每一天!

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


分享到:


相關文章: