本节讨论了关于预处理 C++ 编译器所特有的指令的信息。
预处理程序关键字 pragma 是 C++ 标准的一部分,但每个编译器中,pragma 的形式、内容和含义都是不相同。有关 C++ 编译器可识别的 pragma 列表,请参见A.2.182 -z[ ]arg。
C++ 编译器接受以下形式的 #define 预处理程序指令。
#define identifier (...) replacement_list #define identifier (identifier_list, ...) replacement_list |
如果列出的宏参数以省略号结尾,那么该宏的调用允许使用除了宏参数以外的其他更多参数。其他参数(包括逗号)收集到一个字符串中,宏替换列表中的名称 __VA_ARGS__ 可以引用该字符串。以下示例说明了如何使用可变参数列表的宏。
#define debug(...) fprintf(stderr, __VA_ARGS__) #define showlist(...) puts(#__VA_ARGS__) #define report(test, ...) ((test)?puts(#test):\ printf(__VA_ARGS__)) debug(“Flag”); debug(“X = %d\n”,x); showlist(The first, second, and third items.); report(x>y, “x is %d but y is %d”, x, y); |
其结果如下:
fprintf(stderr, “Flag”); fprintf(stderr, “X = %d\n”, x); puts(“The first, second, and third items.”); ((x>y)?puts(“x>y”):printf(“x is %d but y is %d”, x, y)); |
附录中的表 A–2 介绍了预定义的宏。可以在 #ifdef 这样的预处理程序条件中使用这些值。+p 选项可防止自动定义 sun、unix、sparc 和 i386 预定义宏。
发出警告后,#error 指令不再继续编译。指令原来的行为是发出警告并继续编译。其新行为(和其他编译器保持一致)是发出错误消息并立即停止编译。编译器退出并报告失败。