不宏扩展#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/