C++核心準(zhǔn)則Enum.7: 只在必要時(shí)定義枚舉的底層類型

Enum.7: Specify the underlying type of an enumeration only when necessary
Enum.7:? 只在必要時(shí)定義枚舉的底層類型
Reason(原因)
The default is the easiest to read and write.?int?is the default integer type.?int?is compatible with C?enums.
默認(rèn)的類型更容易讀寫。int是默認(rèn)的整數(shù)類型。int和C語言枚舉類型兼容。
Example(示例)
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
Note(注意)
Specifying the underlying type is necessary in forward declarations of enumerations:
在前置聲明枚舉時(shí)需要定義枚舉的底層類型。
enum Flags : char;
void f(Flags);
// ....
enum flags : char { /* ... */ };
Enforcement(實(shí)施建議)
????
原文鏈接:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#enum7-specify-the-underlying-type-of-an-enumeration-only-when-necessary
覺得本文有幫助?請分享給更多人。
關(guān)注【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
評論
圖片
表情
