首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >C++核心准则Enum.7: 只在必要时定义枚举的底层类型

C++核心准则Enum.7: 只在必要时定义枚举的底层类型

作者头像
面向对象思考
发布于 2020-03-25 09:04:33
发布于 2020-03-25 09:04:33
91300
代码可运行
举报
运行总次数:0
代码可运行

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(示例)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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:

在前置声明枚举时需要定义枚举的底层类型。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
enum Flags : char;

void f(Flags);

// ....

enum flags : char { /* ... */ };
Enforcement(实施建议)

????

原文链接:

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


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

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

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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-03-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
C++核心准则ES.79:使用default处理一般case
Code clarity. Improved opportunities for error detection.
面向对象思考
2020/06/09
3550
C++核心准则Enum.3:枚举类​要比普通的枚举类型好
To minimize surprises: traditional enums convert to int too readily.
面向对象思考
2020/03/25
7390
C++核心准则Enum.1: 枚举类型比宏定义好
Macros do not obey scope and type rules. Also, macro names are removed during preprocessing and so usually don't appear in tools like debuggers.
面向对象思考
2020/03/25
1.3K0
C++核心准则Enum.2: 使用枚举表现一组相关的命名常量
An enumeration shows the enumerators to be related and can be a named type.
面向对象思考
2020/03/25
7200
C++核心准则Enum.6:避免无名枚举
If you can't name an enumeration, the values are not related
面向对象思考
2020/03/25
7010
C++核心准则Enum.5: 不要使用全部大写的枚举值
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#enum5-dont-use-all_caps-for-enumerators
面向对象思考
2020/03/25
8250
C++核心准则C.10:具体类型要好于类继承
C.10: Prefer concrete types over class hierarchies
面向对象思考
2020/03/25
4640
C++核心准则Enum.4:为枚举类型定义运算符以便安全又简单地使用
Convenience of use and avoidance of errors.
面向对象思考
2020/03/25
4290
C++核心准则​NL.16:使用常规的类成员声明顺序
A conventional order of members improves readability.
面向对象思考
2020/12/15
8080
C++核心准则ES.63​:不要分割处理对象
Slicing -- that is, copying only part of an object using assignment or initialization -- most often leads to errors because the object was meant to be considered as a whole. In the rare cases where the slicing was deliberate the code can be surprising.
面向对象思考
2020/05/29
3790
C++核心准则ES.63​:不要分割处理对象
C++核心准则C.183: 不要使用联合体实现双关类型
It is undefined behavior to read a union member with a different type from the one with which it was written. Such punning is invisible, or at least harder to spot than using a named cast. Type punning using a union is a source of errors.
面向对象思考
2020/03/25
5390
C++核心准则C102-109:容器的基本要求
Containers tend to get large; without a move constructor and a copy constructor an object can be expensive to move around, thus tempting people to pass pointers to it around and getting into resource management problems.
面向对象思考
2020/03/25
2850
C++核心准则C.11:让具体类型符合常规
Regular types are easier to understand and reason about than types that are not regular (irregularities requires extra effort to understand and use).
面向对象思考
2020/03/25
2540
C++核心准则C.180:使用联合体节约内存
A union allows a single piece of memory to be used for different types of objects at different times. Consequently, it can be used to save memory when we have several objects that are never used at the same time.
面向对象思考
2020/03/25
4060
C++核心准则C.128:虚函数应该明确定义为virtual,overide或者final
C.128: Virtual functions should specify exactly one of virtual, override, or final
面向对象思考
2020/03/25
5110
C++核心准则ES.70:进行选择时,switch语句比if语句好
Flag if-then-else chains that check against constants (only).
面向对象思考
2020/05/29
3520
C++核心准则ES.70:进行选择时,switch语句比if语句好
C++核心准则C.2:包含不变式时用class,否则用struct定义类
C.2:类包含不变式是使用class定义类,如果数据成员可以独立变更时使用struct定义类。
面向对象思考
2020/03/25
1.3K0
C++核心准则ES.10:每次只定义一个名称
One declaration per line increases readability and avoids mistakes related to the C/C++ grammar. It also leaves room for a more descriptive end-of-line comment.
面向对象思考
2020/04/22
2650
C++核心准则ES.10:每次只定义一个名称
C++核心准则C.3:用类表现接口和实现的区别
An explicit distinction between interface and implementation improves readability and simplifies maintenance.
面向对象思考
2020/03/25
4690
C++核心准则C.6:不要在一条语句内声明类或枚举值的同时又定义该类型的变量
Mixing a type definition and the definition of another entity in the same declaration is confusing and unnecessary.
面向对象思考
2020/03/25
9290
推荐阅读
相关推荐
C++核心准则ES.79:使用default处理一般case
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验