ES.30: Don't use macros for program text manipulation

ES.30: 不要使用宏进行程序中的文本操作

Reason(原因)

Macros are a major source of bugs. Macros don't obey the usual scope and type rules. Macros ensure that the human reader sees something different from what the compiler sees. Macros complicate tool building.

宏是错误的主要来源之一。宏不会遵守通常的范围和类型准则。宏可以为人提供一个和编译器视角有些不同的视角。宏让工具构建变得更复杂。

Example, bad(反面示例)

#define Case break; case   /* BAD */

This innocuous-looking macro makes a single lower case c instead of a C into a bad flow-control bug.

这个看起来无害的宏在将大写C替换为小写c时引入了程序流控制错误。

Note(注意)

This rule does not ban the use of macros for "configuration control" use in #ifdefs, etc.

本准则没有禁止使用宏(如#ifdef等)进行配置控制。

In the future, modules are likely to eliminate the need for macros in configuration control.

将来,模块功能应该可以消除宏在配置控制方面的用途。

Note(注意)

This rule is meant to also discourage use of # for stringification and ## for concatenation. As usual for macros, there are uses that are "mostly harmless", but even these can create problems for tools, such as auto completers, static analyzers, and debuggers. Often the desire to use fancy macros is a sign of an overly complex design. Also, # and ## encourages the definition and use of macros:

本规则也不鼓励使用#和##进行字符串的转换和链接。一般来讲,很多情况下使用宏都是基本无害的,但是即使是这样也会为工具带来困难,例如有些自动补全软件,静态分析软件和调试器等。通常使用花哨的宏定义是过于复杂的设计的信号。#和##也会鼓励宏的定义和使用。

#define CAT(a, b) a ## b
#define STRINGIFY(a) #avoid f(int x, int y)
{string CAT(x, y) = "asdf";   // BAD: hard for tools to handle (and ugly)string sx2 = STRINGIFY(x);// ...
}

There are workarounds for low-level string manipulation using macros. For example:

确实存在需要使用宏定义进行底层字符串操作时可以使用一些变通,例如:

string s = "asdf" "lkjh";   // ordinary string literal concatenationenum E { a, b };template<int x>
constexpr const char* stringify()
{switch (x) {case a: return "a";case b: return "b";}
}void f(int x, int y)
{string sx = stringify<x>();// ...
}

This is not as convenient as a macro to define, but as easy to use, has zero overhead, and is typed and scoped.

这段代码不像定义宏那么容易,但同样方便使用,不存在额外的开销,并且包含类型和范围。

In the future, static reflection is likely to eliminate the last needs for the preprocessor for program text manipulation.

将来,静态反射机制应该可以消除在程序中处理文本时使用预处理器的最后一个需求。

Enforcement(实施建议)

Scream when you see a macro that isn't just used for source control (e.g., #ifdef)

当给你看到宏定义不是用于代码控制(例如#ifdef)时,一定要尖叫。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es30-dont-use-macros-for-program-text-manipulation


觉得本文有帮助?欢迎点赞并分享给更多的人。

更新文章,请关注微信公众号【面向对象思考】

C++核心准则ES.30: 不要使用宏进行程序中的文本操作​相关推荐

  1. C++核心准则ES.56​:只在需要将一个对象显式移动到另外的作用域时使用std::move​

    ES.56: Write std::move() only when you need to explicitly move an object to another scope ES.56:只在需要 ...

  2. C++核心准则ES.40:避免复杂的表达式

    ES.40: Avoid complicated expressions ES.40:避免复杂的表达式 Reason(原因) Complicated expressions are error-pro ...

  3. 无符号数的减法_C++核心准则ES.107:不要使用无符号数下标,使用gsl::index更好

    ES.107: Don't use unsigned for subscripts, prefer gsl::index ES.107:不要使用无符号数下标,使用gsl::index更好 Reason ...

  4. switch语句嵌套if_C++核心准则ES.70:进行选择时,switch语句比if语句好

    ES.70: Prefer a switch-statement to an if-statement when there is a choice ES.70:进行选择时,switch语句比if语句 ...

  5. C++核心准则E.30:不要使用抛异常声明

    E.30: Don't use exception specifications E.20:不要使用抛异常声明 Reason(原因) Exception specifications make err ...

  6. C++核心准则SF.7:不要在头文件中的全局作用域中使用using namespace指令

    SF.7: Don't write using namespace at global scope in a header file SF.7:不要在头文件中的全局作用域中使用using namesp ...

  7. C++核心准则C.82:不要在构造函数或析构函数中调用虚函数

    C.82: Don't call virtual functions in constructors and destructors C.82:不要在构造函数或析构函数中调用虚函数 Reason(原因 ...

  8. Windows核心编程 第十五章 在应用程序中使用虚拟内存

    第1 5章 在应用程序中使用虚拟内存 Wi n d o w s提供了3种进行内存管理的方法,它们是: • 虚拟内存,最适合用来管理大型对象或结构数组. • 内存映射文件,最适合用来管理大型数据流(通常 ...

  9. 开源压缩算法brotli_Google的Brotli压缩算法,C ++核心准则以及更多新闻

    开源压缩算法brotli 在本周的开源新闻综述中,我们将介绍Google的Brotli压缩算法,适用于GitHub的Classroom,C ++ Core Guidelines等! 2015年9月20 ...

最新文章

  1. 索引贴——移动开发(.Net CF 停止更新)
  2. RabbitMQ 发布/订阅
  3. ubuntu——安装和NS3
  4. NS之VGG(Keras):基于Keras的VGG16实现之《复仇者联盟3》灭霸图像风格迁移设计(A Neural Algorithm of Artistic Style)
  5. 深井软岩巷道群支护技术与应用_深井软岩巷道深浅孔帷幕注浆技术
  6. android开发自定义选择器,创建自定义android选择器
  7. [debug] RuntimeError: “nll_loss_forward_reduce_cuda_kernel_2d_index“ not implemented for ‘Int‘
  8. java4特征_java的四大基本特征
  9. 【转】VirtualDOM与diff(Vue实现).MarkDown
  10. tiny4412 编译并烧写系统
  11. 三角网导线平差实例_三角网间接平差示例
  12. LIVE MINI ESP32开发板教程系列(四)NeoPixel + ws2812b实现炫彩显示
  13. CSS揭秘读书笔记-第一章 引言
  14. STM32旋转立方体
  15. 迷你游戏平台开发(二)
  16. 超级无敌屌炸天位运算快读
  17. 10-5 查询选修张老师讲授所有课程的学生
  18. 华为机试 (10/6)
  19. 美国乳品公司试点区块链技术改善食品供应链
  20. 当前时间、既定时间后的时间及时间比较大小(java实现)

热门文章

  1. 《视觉SLAM十四讲 第二版》笔记及课后习题(第三讲)
  2. 计算1加到100的和
  3. BIM模型文件下载——五星级酒店revit模型
  4. 2021年我国航天发射次数有望首次突破40次反超美国
  5. java 读取手机文件_Android获取手机文件夹及文件列表的方法
  6. php教师试讲,教师面试试讲禁忌
  7. Redis集群搭建及java连接redis
  8. mmdvm 接收_MMDVM中继板测试软件MMDVMCal
  9. 图像处理中常用的坐标系转换
  10. 华为正式发布鸿蒙手机可以升级吗,华为鸿蒙OS将于6月2日发布,大批华为荣耀手机可升级,终于等到了...