C++核心准则ES.33:如果必须使用宏定义,命名要有唯一性

共 1020字,需浏览 3分钟

 ·

2020-05-05 23:21


2f585721984cc4108fcbc7294403cf99.webp

ES.33: If you must use macros, give them unique names

ES.33:如果必须使用宏定义,命名要有唯一性


Reason(原因)

Macros do not obey scope rules.

宏定义不会遵守作用域准则。


Example(示例)

#define MYCHAR        /* BAD, will eventually clash with someone else's MYCHAR*/

#define ZCORP_CHAR /* Still evil, but less likely to clash */

Note(注意)

Avoid macros if you can: ES.30, ES.31, and ES.32. However, there are billions of lines of code littered with macros and a long tradition for using and overusing macros. If you are forced to use macros, use long names and supposedly unique prefixes (e.g., your organization's name) to lower the likelihood of a clash.

首先是尽量避免使用宏定义,如果可以的参照ES30,31,32中的方法。然而存在数以十亿计遍布宏定义的代码,也长期存在使用和过度使用宏定义的传统。如果你被迫使用宏定义,使用足够长的名称并可以相信存在唯一性的前缀(例如你所属组织的名称)以便尽量减低冲突的可能性。


Enforcement(实施建议)

Warn against short macro names.

对短的宏名称提出警告。


原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es33-if-you-must-use-macros-give-them-unique-names




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

关注微信公众号【面向对象思考】轻松学习每一天!

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


浏览 64
点赞
评论
收藏
分享

手机扫一扫分享

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

手机扫一扫分享

分享
举报