C预处理器: what is the motivation behind not macro-expanding arguments of#error directive?

标签 c macros language-lawyer c-preprocessor

不宏扩展#error(和#warning)指令参数背后的动机/基本原理是什么?

gcc documentation明确指出:

Neither ‘#error’ nor ‘#warning’ macro-expands its argument.

附注对于 #warning (未标准化),修复方法是使用 #pragma message ( https://stackoverflow.com/a/12638158/1778275 ),它(通常)宏扩展其参数。

UPD。这个问题并不是专门关于为什么 gcc 不执行宏扩展。来自 gcc 文档的引用用作文本示例,其中明确指出#error 和 #warning 宏都不会扩展其参数。我在 C 标准中找不到这个(或类似的)文本。因此,请引用 gcc 文档中的内容。

UPD2。与#error 相比,#include 执行其参数的宏扩展。引自标准 (ISO/IEC 9899:202x (E)):

The preprocessing tokens after include in the directive are processed just as in normal text. (Each identifier currently defined as a macro name is replaced by its replacement list of preprocessing tokens.)

最佳答案

我并不是说这是该标准的动机,但考虑到我对 #error 的许多使用,对其进行宏观扩展会很不方便。
诚然,我的习惯可能是因为不扩张而形成的。

#define MINIMUMCONFIG 1
#ifndef IMPORTANTCONFIG
#error  IMPORTANTCONFIG is undefined!
#error
#if     IMPORTANTCONFIG < MINIMUMCONFIG
#error  IMPORTANTCONFIG lower than MINIMUMCONFIG !
#endif

关于C预处理器: what is the motivation behind not macro-expanding arguments of#error directive?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66171257/

相关文章:

c - 如何创建 "C single-line comment"宏

c - `errno`的语义

c++ - 类模板别名在生成的符号中不应该是可见的,是吗?

使用 Shell 脚本更改 C 程序中的某些内容

带有位图字体的 C 头文件

所有模板实例的 C++ 单一函数指针

C 在不同的头文件中定义具有相同名称的宏?

c - 声明和原型(prototype)的区别

c++ - Code::Block 未创建调试版本?

谁能告诉我为什么在这个 C 代码中出现段错误?