Стандарт C++ 98 (1119566), страница 82
Текст из файла (страница 82)
Thelast token in the sequence is the first new-line character that follows the first token in the sequence.135)preprocessing-file:groupoptgroup:group-partgroup group-partgroup-part:pp-tokensopt new-lineif-sectioncontrol-lineif-section:if-group elif-groupsopt else-groupopt endif-lineif-group:# ifconstant-expression new-line groupopt# ifdef identifier new-line groupopt# ifndef identifier new-line groupoptelif-groups:elif-groupelif-groups elif-groupelif-group:# elifconstant-expression new-line groupopt# elsenew-line groupopt# endifnew-lineelse-group:endif-line:control-line:########includedefinedefineundeflineerrorpragmapp-tokens new-lineidentifier replacement-list new-lineidentifier lparen identifier-listopt ) replacement-list new-lineidentifier new-linepp-tokens new-linepp-tokensopt new-linepp-tokensopt new-linenew-line__________________135) Thus, preprocessing directives are commonly called “lines.” These “lines” have no other syntactic significance, as all white spaceis equivalent except in certain situations during preprocessing (see the # character string literal creation operator in 16.3.2, for example).301ISO/IEC 14882:1998(E)© ISO/IEC16 Preprocessing directives16 Preprocessing directiveslparen:the left-parenthesis character without preceding white-spacereplacement-list:pp-tokensoptpp-tokens:preprocessing-tokenpp-tokens preprocessing-tokennew-line:the new-line character2The only white-space characters that shall appear between preprocessing tokens within a preprocessingdirective (from just after the introducing # preprocessing token through just before the terminating new-linecharacter) are space and horizontal-tab (including spaces that have replaced comments or possibly otherwhite-space characters in translation phase 3).3The implementation can process and skip sections of source files conditionally, include other source files,and replace macros.
These capabilities are called preprocessing, because conceptually they occur beforetranslation of the resulting translation unit.4The preprocessing tokens within a preprocessing directive are not subject to macro expansion unless otherwise stated.16.1 Conditional inclusion1[cpp.cond]The expression that controls conditional inclusion shall be an integral constant expression except that: itshall not contain a cast; identifiers (including those lexically identical to keywords) are interpreted asdescribed below;136) and it may contain unary operator expressions of the formdefined identifierordefined ( identifier )which evaluate to 1 if the identifier is currently defined as a macro name (that is, if it is predefined or if ithas been the subject of a #define preprocessing directive without an intervening #undef directive withthe same subject identifier), zero if it is not.2Each preprocessing token that remains after all macro replacements have occurred shall be in the lexicalform of a token (2.6).3Preprocessing directives of the forms# ifconstant-expression new-line groupopt# elif constant-expression new-line groupoptcheck whether the controlling constant expression evaluates to nonzero.4Prior to evaluation, macro invocations in the list of preprocessing tokens that will become the controllingconstant expression are replaced (except for those macro names modified by the defined unary operator),just as in normal text.
If the token defined is generated as a result of this replacement process or use ofthe defined unary operator does not match one of the two specified forms prior to macro replacement,the behavior is undefined. After all replacements due to macro expansion and the defined unary operatorhave been performed, all remaining identifiers and keywords137), except for true and false, arereplaced with the pp-number 0, and then each preprocessing token is converted into a token.
The resulting__________________136) Because the controlling constant expression is evaluated during translation phase 4, all identifiers either are or are not macronames — there simply are no keywords, enumeration constants, and so on.137) An alternative token (2.5) is not an identifier, even when its spelling consists entirely of letters and underscores. Therefore it isnot subject to this replacement.302© ISO/IECISO/IEC 14882:1998(E)16 Preprocessing directives16.1 Conditional inclusiontokens comprise the controlling constant expression which is evaluated according to the rules of 5.19 usingarithmetic that has at least the ranges specified in 18.2, except that int and unsigned int act as if theyhave the same representation as, respectively, long and unsigned long.
This includes interpretingcharacter literals, which may involve converting escape sequences into execution character set members.Whether the numeric value for these character literals matches the value obtained when an identical character literal occurs in an expression (other than within a #if or #elif directive) is implementationdefined.138) Also, whether a single-character character literal may have a negative value isimplementation-defined. Each subexpression with type bool is subjected to integral promotion beforeprocessing continues.5Preprocessing directives of the forms# ifdef identifier new-line groupopt# ifndef identifier new-line groupoptcheck whether the identifier is or is not currently defined as a macro name.
Their conditions are equivalentto #if defined identifier and #if !defined identifier respectively.6Each directive’s condition is checked in order. If it evaluates to false (zero), the group that it controls isskipped: directives are processed only through the name that determines the directive in order to keep trackof the level of nested conditionals; the rest of the directives’ preprocessing tokens are ignored, as are theother preprocessing tokens in the group. Only the first group whose control condition evaluates to true(nonzero) is processed. If none of the conditions evaluates to true, and there is a #else directive, thegroup controlled by the #else is processed; lacking a #else directive, all the groups until the #endifare skipped.139)16.2 Source file inclusion[cpp.include]1A #include directive shall identify a header or source file that can be processed by the implementation.2A preprocessing directive of the form# include <h-char-sequence> new-linesearches a sequence of implementation-defined places for a header identified uniquely by the specifiedsequence between the < and > delimiters, and causes the replacement of that directive by the entire contentsof the header.
How the places are specified or the header identified is implementation-defined.3A preprocessing directive of the form# include "q-char-sequence" new-linecauses the replacement of that directive by the entire contents of the source file identified by the specifiedsequence between the " delimiters. The named source file is searched for in an implementation-definedmanner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read# include <h-char-sequence> new-linewith the identical contained sequence (including > characters, if any) from the original directive.4A preprocessing directive of the form# include pp-tokens new-line(that does not match one of the two previous forms) is permitted. The preprocessing tokens after include__________________138) Thus, the constant expression in the following #if directive and if statement is not guaranteed to evaluate to the same value inthese two contexts.#if ’z’ - ’a’ = = 25if (’z’ - ’a’ = = 25)139) As indicated by the syntax, a preprocessing token shall not follow a #else or #endif directive before the terminating new-linecharacter.
However, comments may appear anywhere in a source file, including within a preprocessing directive.303ISO/IEC 14882:1998(E)© ISO/IEC16.2 Source file inclusion16 Preprocessing directivesin the directive are processed just as in normal text (each identifier currently defined as a macro name isreplaced by its replacement list of preprocessing tokens). If the directive resulting after all replacementsdoes not match one of the two previous forms, the behavior is undefined.140) The method by which asequence of preprocessing tokens between a < and a > preprocessing token pair or a pair of " characters iscombined into a single header name preprocessing token is implementation-defined.5The mapping between the delimited sequence and the external source file name is implementation-defined.The implementation provides unique mappings for sequences consisting of one or more nondigits (2.10)followed by a period (.) and a single nondigit.
The implementation may ignore the distinctions of alphabetical case.6A #include preprocessing directive may appear in a source file that has been read because of a#include directive in another file, up to an implementation-defined nesting limit.7[Example: The most common uses of #include preprocessing directives are as in the following:#include <stdio.h>#include "myprog.h"—end example]8[Example: Here is a macro-replaced #include directive:#if VERSION = = 1#define INCFILE#elif VERSION = = 2#define INCFILE#else#define INCFILE#endif#include INCFILE"vers1.h""vers2.h"/* and so on */"versN.h"—end example]16.3 Macro replacement[cpp.replace]1Two replacement lists are identical if and only if the preprocessing tokens in both have the same number,ordering, spelling, and white-space separation, where all white-space separations are considered identical.2An identifier currently defined as a macro without use of lparen (an object-like macro) may be redefined byanother #define preprocessing directive provided that the second definition is an object-like macro definition and the two replacement lists are identical, otherwise the program is ill-formed.3An identifier currently defined as a macro using lparen (a function-like macro) may be redefined by another#define preprocessing directive provided that the second definition is a function-like macro definitionthat has the same number and spelling of parameters, and the two replacement lists are identical, otherwisethe program is ill-formed.4The number of arguments in an invocation of a function-like macro shall agree with the number of parameters in the macro definition, and there shall exist a ) preprocessing token that terminates the invocation.5A parameter identifier in a function-like macro shall be uniquely declared within its scope.6The identifier immediately following the define is called the macro name.
There is one name space formacro names. Any white-space characters preceding or following the replacement list of preprocessingtokens are not considered part of the replacement list for either form of macro.7If a # preprocessing token, followed by an identifier, occurs lexically at the point at which a preprocessingdirective could begin, the identifier is not subject to macro replacement.__________________140) Note that adjacent string literals are not concatenated into a single string literal (see the translation phases in 2.1); thus, an expansion that results in two string literals is an invalid directive.304© ISO/IECISO/IEC 14882:1998(E)16 Preprocessing directives816.3 Macro replacementA preprocessing directive of the form# define identifier replacement-list new-linedefines an object-like macro that causes each subsequent instance of the macro name141) to be replaced bythe replacement list of preprocessing tokens that constitute the remainder of the directive.142) The replacement list is then rescanned for more macro names as specified below.9A preprocessing directive of the form# define identifier lparen identifier-listopt ) replacement-list new-linedefines a function-like macro with parameters, similar syntactically to a function call.