简体   繁体   English

在预处理指令中使用变量

[英]Using variables in the preprocessor directives

which global variable can be used in the preprocessor directive file.cpp 可以在预处理程序指令文件中使用哪个全局变量

int variable = 1;
#if variable >= 1
    int a = 0;
#else 
    int a = 1;
#endif

or 要么

file.cpp file.cpp

const int variable = 1;
#if variable >= 1
    int a = 0;
#else 
    int a = 1;
#endif

or file.cpp 或file.cpp

#include "header.h"
// extern in variable; in the header.h
#if variable >= 1
    int a = 0;
#else 
    int a = 1;
#endif

What are the rules which governs using the variables in the proprocessor directive? 在proprocessor指令中使用变量的规则有哪些? If a variable which can be consant folded, can it be used in the #if/#elif#else directives? 如果一个变量可以被允许折叠,是否可以在#if /#elif#else指令中使用它?

Sorry, you can't do this at all. 抱歉,您根本无法执行此操作。 Variables are not visible to the preprocessor. 变量对预处理器不可见。 The preprocessor is at its heart a text manipulator. 预处理程序的核心是文本操纵器。 The only values it can see are ones defined with #define , not variables. 它只能看到用#define定义的值,而不是变量。

Only macros defined with #define will have their expected value in an #if . 只有使用#define定义的宏才会在#if具有其期望值。 All other symbols (more precisely, all identifiers that remain on an #if line after macro expansion, except defined and, in C++, certain "alternative spellings" of arithmetic operators, such as and , or , bitand , bitor , and compl ) are interpreted as having the value 0. 所有其他符号(更确切地说,在宏扩展之后保留在#if行上的所有标识符,除了defined且在C ++中,算术运算符的某些“替代拼写”,例如and orbitandbitorcompl )是解释为值为0。

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

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