Conditional inclusion
来自cppreference.com
< cpp | preprocessor
![]() |
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
支持有条件的预处理器的编译源文件的部分。这种行为是控制
#if
,#else
,#elif
,#ifdef
,#ifndef
和#endif
指令.原文:
The preprocessor supports conditional compilation of parts of source file. This behavior is controlled by
#if
, #else
, #elif
, #ifdef
, #ifndef
and #endif
directives.目录 |
[编辑] 语法
#if expression
|
|||||||||
#elif expression
|
|||||||||
#ifdef expression
|
|||||||||
#ifndef expression
|
|||||||||
#else expression
|
|||||||||
#endif expression
|
|||||||||
[编辑] 解释
的条件的预处理块开始
#if
,#ifdef
或#ifndef
指令,然后任选地包括任意数量的#elif
指令,然后任选地包括至多1#else
指令和终止与#endif
指令。内的任何条件预处理块分别进行处理.原文:
The conditional preprocessing block starts with
#if
, #ifdef
or #ifndef
directive, then optionally includes any number of #elif
directives, then optionally includes at most one #else
directive and is terminated with #endif
directive. Any inner conditional preprocessing blocks are processed separately.#if
,#elif
,#else
,#ifdef
#ifndef
指令控制的代码块,直到第一个#elif
,#else
,#endif
指令不属于任何内部条件预处理块. 原文:
Each of
#if
, #elif
, #else
, #ifdef
and #ifndef
directives control code block until first #elif
, #else
, #endif
directive not belonging to any inner conditional preprocessing blocks. #if
,#ifdef
#ifndef
指令测试指定条件(见下文),如果它的值为true,编译控制的代码块。在这种情况下,后续的#else
和#elif
指令将被忽略。否则,如果指定的条件判断为假,则控制的代码块被跳过,随后的#else
或#elif
指令(如果有的话)处理。在前者的情况下,由#else
指令控制的代码块被无条件地编译。在后者的情况下,#elif
指令的作用,如果它是#if
指令:检查条件,编译或跳过控制基于该结果的代码块,并且在后者的情况下处理后续#elif
和#else
指令。 #endif
指令终止的条件预处理块.原文:
#if
, #ifdef
and #ifndef
directives test the specified condition (see below) and if it evaluates to true, compiles the controlled code block. In that case subsequent #else
and #elif
directives are ignored. Otherwise, if the specified condition evaluates false, the controlled code block is skipped and the subsequent #else
or #elif
directive (if any) is processed. In the former case, the code block controlled by the #else
directive is unconditionally compiled. In the latter case, the #elif
directive acts as if it was #if
directive: checks for condition, compiles or skips the controlled code block based on the result, and in the latter case processes subsequent #elif
and #else
directives. The conditional preprocessing block is terminated by #endif
directive.[编辑] 状况评估
[编辑] #if, #elif
expression是一个常量表达式,只用文字定义的标识符,使用
#define
指令。任何标识,这是没有文字的,不使用 #define
指令定义,计算结果为“0”. 表达式可以包含一元运算符的形式
defined
identifier或defined (
identifier)
1定义使用identifier指令和“0”,否则返回 #define
。如果expression计算结果为非零值,包括控制代码块,否则跳过。如果有任何使用的标识符是不是一个常数,它会被替换0.原文:
The expression may contain unary operators in form
defined
identifier or defined (
identifier)
which return 1 if the identifier was defined using #define
directive and 0 otherwise. If the expression evaluates to nonzero value, the controlled code block is included and skipped otherwise. If any used identifier is not a constant, it is replaced with 0.[编辑] #ifdef, #ifndef
#ifdef
identifier基本上等同于#if defined(
identifier)
.原文:
#ifdef
identifier is essentially equivalent to #if defined(
identifier)
.#ifndef
identifier基本上等同于#if !defined(
identifier)
.原文:
#ifndef
identifier is essentially equivalent to #if !defined(
identifier)
.[编辑] 示例
#define ABCD 2 #include <iostream> int main() { #ifdef ABCD std::cout << "1: yes\n"; #else std::cout << "1: no\n"; #endif #ifndef ABCD std::cout << "2: no1\n"; #elif ABCD == 2 std::cout << "2: yes\n"; #else std::cout << "2: no2\n"; #endif #if !defined(DCBA) && (ABCD < 2*4-3) std::cout << "3: yes\n"; #endif }
输出:
1: yes 2: yes 3: yes