简体   繁体   English

C++ 一行中的多个预处理器指令

[英]Multiple preprocessor directives on one line in C++

A hypothetical question: Is it possible to have a C++ program, which includes preprocessor directives, entirely on one line?一个假设的问题:是否有可能将包含预处理器指令的 C++ 程序完全放在一行中?

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?此外,是否存在不可能在一行中构建的指令组合?

If this is compiler-specific then both VC++ and GCC answers are welcome.如果这是特定于编译器的,那么欢迎使用 VC++ 和 GCC 答案。

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:预处理指令必须以换行符结束,所以这实际上是一个单独的预处理指令,它定义了一个类对象宏,名为foo ,扩展为以下标记序列:

# ifdef foo # define bar # endif

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.以后在源代码中使用名称foo (直到它被#undef编辑)将扩展为此,但是在扩展宏之后,生成的标记不会被评估为预处理指令。

This is not compiler-specific;这不是特定于编译器的; this behavior is defined by the C and C++ standards.此行为由 C 和 C++ 标准定义。

Preprocessor directives are somewhat different than language statements, which are terminated by ;预处理器指令与语言语句有些不同,语言语句以;终止。 and use whitespace to delimit tokens.并使用空格来分隔标记。 In the case of the preprocessor, the directive is terminated by a newline so it's impossible to do what you're attempting using the C++ language itself.在预处理器的情况下,该指令以换行符终止,因此不可能使用 C++ 语言本身来执行您尝试执行的操作。

One way you could kind of simulate this is to put your desired lines into a separate header file and then #include it where you want.您可以模拟这种情况的一种方法是将所需的行放入单独的头文件中,然后将其#include到您想要的位置。 The separate header still has to have each directive on one line, but the point where you include it is just a single line, effectively doing what you asked.单独的标头仍然必须将每个指令放在一行中,但是您包含它的地方只是一行,可以有效地执行您的要求。

Another way to accomplish something like that is to have a pre-C++ file that you use an external process to process into a C++ source file prior to compiling with your C++ compiler.完成类似任务的另一种方法是在使用 C++ 编译器进行编译之前,使用外部进程将 C++ 前文件处理成 C++ 源文件。 This is probably rather more trouble than it's worth.这可能比它的价值更麻烦。

A hypothetical question: Is it possible to have a C++ program, which includes preprocessor directives, entirely on one line?一个假设的问题:是否有可能在一行中包含一个包含预处理器指令的 C++ 程序?

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?此外,是否存在无法在一行中构建的指令组合?

If this is compiler-specific then both VC++ and GCC answers are welcome.如果这是特定于编译器的,那么欢迎 VC++ 和 GCC 答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM