问题描述
一个假设的问题:是否可能有一个C ++程序,包括预处理指令,完全在一行上?
A hypothetical question: Is it possible to have a C++ program, which includes preprocessor directives, entirely on one line?
这样的行看起来像这样:
Such a line would look like this:
#define foo #ifdef foo #define bar #endif
这样的行的语义是什么?
What are the semantics of such a line?
此外,是否存在无法构造的指令组合?
Further, are there any combinations of directives which are impossible to construct on one line?
如果这是特定于编译器的,则欢迎使用VC ++和GCC答案。
If this is compiler-specific then both VC++ and GCC answers are welcome.
推荐答案
预处理指令必须以换行符终止,因此实际上这是一个单一的预处理指令,该指令定义了一个类似于对象的宏,名为 foo
,将扩展为以下标记序列:
A preprocessing directive must be terminated by a newline, so this is actually a single preprocessing directive that defines an object-like macro, named foo
, that expands to the following token sequence:
# ifdef foo # define bar # endif
以后在源中使用名称 foo
的任何用法(直到 #undef
ed)将扩展为此,但
Any later use of the name foo
in the source (until it is #undef
ed) will expand to this, but after the macro is expanded, the resulting tokens are not evaluated as a preprocessing directive.
这不是特定于编译器的;而是将宏扩展后,才将它们作为预处理指令进行评估。这种行为由C和C ++标准定义。
This is not compiler-specific; this behavior is defined by the C and C++ standards.
这篇关于在C ++的一行上有多个预处理器指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!