前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则ES.33:如果必须使用宏定义,命名要有唯一性

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

作者头像
面向对象思考
发布2020-05-20 00:17:11
2100
发布2020-05-20 00:17:11
举报

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

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

Reason(原因)

Macros do not obey scope rules.

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

Example(示例)

代码语言:javascript
复制
#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-name

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ES.33: If you must use macros, give them unique names
  • Reason(原因)
    • Note(注意)
      • Enforcement(实施建议)
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档