Conditional inclusion
De cppreference.com
< cpp | preprocessor
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
El preprocesador soporta compilación condicional de algunas partes del archivo de origen. Este comportamiento está controlado por
#if, #else, #elif, #ifdef, #ifndef y #endif directivas .Original:
The preprocessor supports conditional compilation of parts of source file. This behavior is controlled by
#if, #else, #elif, #ifdef, #ifndef and #endif directives.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Contenido |
[editar] Sintaxis
#if expression
|
|||||||||
#elif expression
|
|||||||||
#ifdef expression
|
|||||||||
#ifndef expression
|
|||||||||
#else expression
|
|||||||||
#endif expression
|
|||||||||
[editar] Explicación
El bloque de preprocesamiento condicional comienza con
#if, #ifdef o directiva #ifndef, luego opcionalmente incluye cualquier número de directivas #elif, a continuación, opcionalmente, incluye la mayoría menos una directiva #else y se termina con la Directiva #endif. Todos los bloques interiores condicionales preprocesamiento se procesan por separado .Original:
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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Cada uno de
#if, #elif, #else, #ifdef #ifndef y control de directivas bloque de código hasta que primero #elif, #else, directiva #endif que no pertenecen a ningún bloque de preprocesamiento condicionales interiores . Original:
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. The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#if, #ifdef y #ifndef directivas probar la condición especificada (ver abajo) y si se evalúa como verdadera, compila el bloque de código controlado. En ese caso posterior #else y directivas #elif se ignoran. De lo contrario, si la condición especificada se evalúa falsa, el bloque de código controlado se omite y la subsiguiente #else o #elif directiva (si los hay) se procesa. En el primer caso, el bloque de código controlado por la directiva #else está incondicionalmente compilado. En este último caso, la directiva #elif actúa como si se tratara de directiva #if: comprueba condición, compila o se salta el bloque de código controlado, basado en el resultado, y en este último caso los procesos posteriores #elif y #else directivas. El bloque de preprocesamiento condicional se termina por la directiva #endif .Original:
#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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[editar] Estado de la evaluación
[editar] #if, #elif
El expression es una expresión constante, utilizando sólo literales y los identificadores, que se define mediante directiva
#define. Cualquier identificador, que no es literal, no define con #define directiva, se evalúa como 0 . Original:
The expression is a constant expression, using only literales and identifiers, defined using
#define directive. Any identifier, which is not literal, non defined using #define directive, evaluates to 0. The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
La expresión puede contener operadores unarios en forma
defined identifier o defined (identifier) que volver 1 si el identifier se definió utilizando directiva #define y 0 en caso contrario'. Si el expression evalúa en el valor distinto de cero, el bloque de código controlado está incluido y salta lo contrario. Si cualquier identificador utilizado no es una constante, que se sustituye con 0 .Original:
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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[editar] #ifdef, #ifndef
Comprueba si el identificador se definió utilizando directiva
#define . Original:
Checks if the identifier was defined using
#define directive. The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#ifdef identifier es esencialmente equivalente a #if defined( identifier) .Original:
#ifdef identifier is essentially equivalent to #if defined( identifier).The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#ifndef identifier es esencialmente equivalente a #if !defined( identifier) .Original:
#ifndef identifier is essentially equivalent to #if !defined( identifier).The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[editar] Ejemplo
#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 }
Output:
1: yes 2: yes 3: yes