[英]Using variables in the preprocessor directives
可以在预处理程序指令文件中使用哪个全局变量
int variable = 1;
#if variable >= 1
int a = 0;
#else
int a = 1;
#endif
要么
file.cpp
const int variable = 1;
#if variable >= 1
int a = 0;
#else
int a = 1;
#endif
或file.cpp
#include "header.h"
// extern in variable; in the header.h
#if variable >= 1
int a = 0;
#else
int a = 1;
#endif
在proprocessor指令中使用变量的规则有哪些? 如果一个变量可以被允许折叠,是否可以在#if /#elif#else指令中使用它?
抱歉,您根本无法执行此操作。 变量对预处理器不可见。 预处理程序的核心是文本操纵器。 它只能看到用#define
定义的值,而不是变量。
只有使用#define
定义的宏才会在#if
具有其期望值。 所有其他符号(更确切地说,在宏扩展之后保留在#if
行上的所有标识符,除了defined
且在C ++中,算术运算符的某些“替代拼写”,例如and
or
, bitand
, bitor
和compl
)是解释为值为0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.