572 lines
29 KiB
Markdown
572 lines
29 KiB
Markdown
[cpp.replace]
|
||
|
||
# 15 Preprocessing directives [[cpp]](./#cpp)
|
||
|
||
## 15.7 Macro replacement [cpp.replace]
|
||
|
||
### [15.7.1](#general) General [[cpp.replace.general]](cpp.replace.general)
|
||
|
||
[1](#general-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1443)
|
||
|
||
Two replacement lists are identical if and only if
|
||
the preprocessing tokens in both have
|
||
the same number, ordering, spelling, and whitespace separation,
|
||
where all whitespace separations are considered identical[.](#general-1.sentence-1)
|
||
|
||
[2](#general-2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1450)
|
||
|
||
An identifier currently defined as anobject-like macro (see below) may be redefined by another#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[.](#general-2.sentence-1)
|
||
|
||
Likewise, an identifier currently defined as afunction-like macro (see below) may be redefined by another#define preprocessing directive provided that the second definition is a
|
||
function-like macro definition that has the same number and spelling
|
||
of parameters,
|
||
and the two replacement lists are identical,
|
||
otherwise the program is ill-formed[.](#general-2.sentence-2)
|
||
|
||
[3](#general-3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1468)
|
||
|
||
[*Example [1](#general-example-1)*:
|
||
|
||
The following sequence is valid:#define OBJ_LIKE (1-1)#define OBJ_LIKE /* whitespace */ (1-1) /* other */#define FUNC_LIKE(a) ( a )#define FUNC_LIKE( a )( /* note the whitespace */ \
|
||
a /* other stuff on this line
|
||
*/ )
|
||
|
||
But the following redefinitions are invalid:#define OBJ_LIKE (0) // different token sequence#define OBJ_LIKE (1 - 1) // different whitespace#define FUNC_LIKE(b) ( a ) // different parameter usage#define FUNC_LIKE(b) ( b ) // different parameter spelling
|
||
|
||
â *end example*]
|
||
|
||
[4](#general-4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1488)
|
||
|
||
There shall be whitespace between the identifier and the replacement list
|
||
in the definition of an object-like macro[.](#general-4.sentence-1)
|
||
|
||
[5](#general-5)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1493)
|
||
|
||
If the [*identifier-list*](cpp.pre#nt:identifier-list "15.1 Preamble [cpp.pre]") in the macro definition does not end with
|
||
an ellipsis, the number of arguments (including those arguments consisting
|
||
of no preprocessing tokens)
|
||
in an invocation of a function-like macro shall
|
||
equal the number of parameters in the macro definition[.](#general-5.sentence-1)
|
||
|
||
Otherwise, there shall be at least as many arguments in the invocation as there are
|
||
parameters in the macro definition (excluding the ...)[.](#general-5.sentence-2)
|
||
|
||
There
|
||
shall exist a) preprocessing token that terminates the invocation[.](#general-5.sentence-3)
|
||
|
||
[6](#general-6)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1505)
|
||
|
||
The identifiers __VA_ARGS__ and __VA_OPT__ shall occur only in the [*replacement-list*](cpp.pre#nt:replacement-list "15.1 Preamble [cpp.pre]") of a function-like macro that uses the ellipsis notation in the parameters[.](#general-6.sentence-1)
|
||
|
||
[7](#general-7)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1512)
|
||
|
||
A parameter identifier in a function-like macro
|
||
shall be uniquely declared within its scope[.](#general-7.sentence-1)
|
||
|
||
[8](#general-8)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1516)
|
||
|
||
The identifier immediately following thedefine is called the[*macro name*](#def:macro,name "15.7.1 General [cpp.replace.general]")[.](#general-8.sentence-1)
|
||
|
||
There is one name space for macro names[.](#general-8.sentence-2)
|
||
|
||
Any whitespace characters preceding or following the
|
||
replacement list of preprocessing tokens are not considered
|
||
part of the replacement list for either form of macro[.](#general-8.sentence-3)
|
||
|
||
[9](#general-9)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1527)
|
||
|
||
A translation unit shall not #define or #undef names lexically identical
|
||
to keywords,
|
||
to the identifiers listed in Table [4](lex.name#tab:lex.name.special "Table 4: Identifiers with special meaning"), or
|
||
to the [*attribute-token*](dcl.attr.grammar#nt:attribute-token "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]")*s* described in [[dcl.attr]](dcl.attr "9.13 Attributes"),
|
||
except that the names likely and unlikely may be
|
||
defined as function-like macros[.](#general-9.sentence-1)
|
||
|
||
[10](#general-10)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1537)
|
||
|
||
If a# preprocessing token,
|
||
followed by an identifier,
|
||
occurs lexically
|
||
at the point at which a preprocessing directive can begin,
|
||
the identifier is not subject to macro replacement[.](#general-10.sentence-1)
|
||
|
||
[11](#general-11)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1547)
|
||
|
||
A preprocessing directive of the form
|
||
|
||
# define [*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]") [*replacement-list*](cpp.pre#nt:replacement-list "15.1 Preamble [cpp.pre]") [*new-line*](cpp.pre#nt:new-line "15.1 Preamble [cpp.pre]")
|
||
|
||
defines an[*object-like macro*](#def:macro,object-like "15.7.1 General [cpp.replace.general]") that
|
||
causes each subsequent instance of the macro name[125](#footnote-125 "Since, by macro-replacement time, all character-literals and string-literals are preprocessing tokens, not sequences possibly containing identifier-like subsequences (see [lex.phases], translation phases), they are never scanned for macro names or parameters.") to be replaced by the replacement list of preprocessing tokens
|
||
that constitute the remainder of the directive[.](#general-11.sentence-1)[126](#footnote-126 "An alternative token ([lex.digraph]) is not an identifier, even when its spelling consists entirely of letters and underscores. Therefore it is not possible to define a macro whose name is the same as that of an alternative token.")
|
||
|
||
The replacement list is then rescanned for more macro names as
|
||
specified below[.](#general-11.sentence-2)
|
||
|
||
[12](#general-12)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1574)
|
||
|
||
[*Example [2](#general-example-2)*:
|
||
|
||
The simplest use of this facility is to define a âmanifest constantâ,
|
||
as in#define TABSIZE 100int table[TABSIZE];
|
||
|
||
â *end example*]
|
||
|
||
[13](#general-13)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1584)
|
||
|
||
A preprocessing directive of the form
|
||
|
||
# define [*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]") [*lparen*](cpp.pre#nt:lparen "15.1 Preamble [cpp.pre]") [*identifier-list*](cpp.pre#nt:identifier-list "15.1 Preamble [cpp.pre]")opt ) [*replacement-list*](cpp.pre#nt:replacement-list "15.1 Preamble [cpp.pre]") [*new-line*](cpp.pre#nt:new-line "15.1 Preamble [cpp.pre]")
|
||
# define [*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]") [*lparen*](cpp.pre#nt:lparen "15.1 Preamble [cpp.pre]") ... ) [*replacement-list*](cpp.pre#nt:replacement-list "15.1 Preamble [cpp.pre]") [*new-line*](cpp.pre#nt:new-line "15.1 Preamble [cpp.pre]")
|
||
# define [*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]") [*lparen*](cpp.pre#nt:lparen "15.1 Preamble [cpp.pre]") [*identifier-list*](cpp.pre#nt:identifier-list "15.1 Preamble [cpp.pre]") , ... ) [*replacement-list*](cpp.pre#nt:replacement-list "15.1 Preamble [cpp.pre]") [*new-line*](cpp.pre#nt:new-line "15.1 Preamble [cpp.pre]")
|
||
|
||
defines a [*function-like macro*](#def:macro,function-like "15.7.1 General [cpp.replace.general]") with parameters, whose use is
|
||
similar syntactically to a function call[.](#general-13.sentence-1)
|
||
|
||
The parametersare specified by the optional list of identifiers[.](#general-13.sentence-2)
|
||
|
||
Each subsequent instance of the function-like macro name followed by a( as the next preprocessing token
|
||
introduces the sequence of preprocessing tokens that is replaced
|
||
by the replacement list in the definition
|
||
(an invocation of the macro)[.](#general-13.sentence-3)
|
||
|
||
The replaced sequence of preprocessing tokens is terminated by the matching) preprocessing token, skipping intervening matched pairs of left and
|
||
right parenthesis preprocessing tokens[.](#general-13.sentence-4)
|
||
|
||
Within the sequence of preprocessing tokens making up an invocation
|
||
of a function-like macro,
|
||
new-line is considered a normal whitespace character[.](#general-13.sentence-5)
|
||
|
||
[14](#general-14)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1612)
|
||
|
||
The sequence of preprocessing tokens
|
||
bounded by the outside-most matching parentheses
|
||
forms the list of arguments for the function-like macro[.](#general-14.sentence-1)
|
||
|
||
The individual arguments within the list
|
||
are separated by comma preprocessing tokens,
|
||
but comma preprocessing tokens between matching
|
||
inner parentheses do not separate arguments[.](#general-14.sentence-2)
|
||
|
||
If there are sequences of preprocessing tokens within the list of
|
||
arguments that would otherwise act as preprocessing directives,[127](#footnote-127 "A conditionally-supported-directive is a preprocessing directive regardless of whether the implementation supports it.") the program is ill-formed[.](#general-14.sentence-3)
|
||
|
||
[15](#general-15)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1628)
|
||
|
||
[*Example [3](#general-example-3)*:
|
||
|
||
The following defines a function-like
|
||
macro whose value is the maximum of its arguments[.](#general-15.sentence-1)
|
||
|
||
It has the disadvantages of evaluating one or the other of its arguments
|
||
a second time
|
||
(includingside effects)
|
||
and generating more code than a function if invoked several times[.](#general-15.sentence-2)
|
||
|
||
It also cannot have its address taken,
|
||
as it has none[.](#general-15.sentence-3)
|
||
|
||
#define max(a, b) ((a) > (b) ? (a) : (b))
|
||
|
||
The parentheses ensure that the arguments and
|
||
the resulting expression are bound properly[.](#general-15.sentence-4)
|
||
|
||
â *end example*]
|
||
|
||
[16](#general-16)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1649)
|
||
|
||
If there is a ... immediately preceding the ) in the
|
||
function-like macro
|
||
definition, then the trailing arguments (if any), including any separating comma preprocessing
|
||
tokens, are merged to form a single item: the [*variable arguments*](#def:variable_arguments "15.7.1 General [cpp.replace.general]")[.](#general-16.sentence-1)
|
||
|
||
The number of
|
||
arguments so combined is such that, following merger, the number of arguments is
|
||
either equal to or
|
||
one more than the number of parameters in the macro definition (excluding the...)[.](#general-16.sentence-2)
|
||
|
||
[125)](#footnote-125)[125)](#footnoteref-125)
|
||
|
||
Since, by macro-replacement time,
|
||
all [*character-literal*](lex.ccon#nt:character-literal "5.13.3 Character literals [lex.ccon]")*s* and [*string-literal*](lex.string#nt:string-literal "5.13.5 String literals [lex.string]")*s* are preprocessing tokens,
|
||
not sequences possibly containing identifier-like subsequences
|
||
(see [[lex.phases]](lex.phases "5.2 Phases of translation"), translation phases),
|
||
they are never scanned for macro names or parameters[.](#footnote-125.sentence-1)
|
||
|
||
[126)](#footnote-126)[126)](#footnoteref-126)
|
||
|
||
An alternative token ([[lex.digraph]](lex.digraph "5.9 Alternative tokens")) is not an identifier,
|
||
even when its spelling consists entirely of letters and underscores[.](#footnote-126.sentence-1)
|
||
|
||
Therefore it is not possible to define a macro
|
||
whose name is the same as that of an alternative token[.](#footnote-126.sentence-2)
|
||
|
||
[127)](#footnote-127)[127)](#footnoteref-127)
|
||
|
||
A [*conditionally-supported-directive*](cpp.pre#nt:conditionally-supported-directive "15.1 Preamble [cpp.pre]") is a preprocessing directive regardless of whether the implementation supports it[.](#footnote-127.sentence-1)
|
||
|
||
### [15.7.2](#cpp.subst) Argument substitution [[cpp.subst]](cpp.subst)
|
||
|
||
[va-opt-replacement:](#nt:va-opt-replacement "15.7.2 Argument substitution [cpp.subst]")
|
||
__VA_OPT__ ( [*pp-tokens*](cpp.pre#nt:pp-tokens "15.1 Preamble [cpp.pre]")opt )
|
||
|
||
[1](#cpp.subst-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1670)
|
||
|
||
After the arguments for the invocation of a function-like macro have
|
||
been identified, argument substitution takes place[.](#cpp.subst-1.sentence-1)
|
||
|
||
For each parameter in the replacement list that is neither
|
||
preceded by a # or ## preprocessing token nor
|
||
followed by a ## preprocessing token, the preprocessing tokens
|
||
naming the parameter are replaced by a preprocessing token sequence determined as follows:
|
||
|
||
- [(1.1)](#cpp.subst-1.1)
|
||
|
||
If the parameter is of the form [*va-opt-replacement*](#nt:va-opt-replacement "15.7.2 Argument substitution [cpp.subst]"),
|
||
the replacement preprocessing tokens are the
|
||
preprocessing token sequence for the corresponding argument,
|
||
as specified below[.](#cpp.subst-1.1.sentence-1)
|
||
|
||
- [(1.2)](#cpp.subst-1.2)
|
||
|
||
Otherwise, the replacement preprocessing tokens are the
|
||
preprocessing tokens of corresponding argument after all
|
||
macros contained therein have been expanded[.](#cpp.subst-1.2.sentence-1)
|
||
The argument's
|
||
preprocessing tokens are completely macro replaced before
|
||
being substituted as if they formed the rest of the preprocessing
|
||
translation unit with no other preprocessing tokens being available[.](#cpp.subst-1.2.sentence-2)
|
||
|
||
[*Example [1](#cpp.subst-example-1)*: #define LPAREN() (#define G(Q) 42#define F(R, X, ...) __VA_OPT__(G R X) )int x = F(LPAREN(), 0, <:-); // replaced by int x = 42; â *end example*]
|
||
|
||
[2](#cpp.subst-2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1700)
|
||
|
||
An identifier __VA_ARGS__ that occurs in the replacement list
|
||
shall be treated as if it were a parameter, and the variable arguments shall form
|
||
the preprocessing tokens used to replace it[.](#cpp.subst-2.sentence-1)
|
||
|
||
[3](#cpp.subst-3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1706)
|
||
|
||
[*Example [2](#cpp.subst-example-2)*:
|
||
|
||
#define debug(...) fprintf(stderr, __VA_ARGS__)#define showlist(...) puts(#__VA_ARGS__)#define report(test, ...) ((test) ? puts(#test) : printf(__VA_ARGS__)) debug("Flag");
|
||
debug("X = %d\n", x);
|
||
showlist(The first, second, and third items.);
|
||
report(x>y, "x is %d but y is %d", x, y); results infprintf(stderr, "Flag");
|
||
fprintf(stderr, "X = %d\n", x);
|
||
puts("The first, second, and third items.");((x>y) ? puts("x>y") : printf("x is %d but y is %d", x, y));
|
||
|
||
â *end example*]
|
||
|
||
[4](#cpp.subst-4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1726)
|
||
|
||
The identifier __VA_OPT__ shall always occur as part of the preprocessing token sequence[*va-opt-replacement*](#nt:va-opt-replacement "15.7.2 Argument substitution [cpp.subst]");
|
||
its closing ) is determined by skipping
|
||
intervening pairs of matching left and right parentheses
|
||
in its [*pp-tokens*](cpp.pre#nt:pp-tokens "15.1 Preamble [cpp.pre]")[.](#cpp.subst-4.sentence-1)
|
||
|
||
The [*pp-tokens*](cpp.pre#nt:pp-tokens "15.1 Preamble [cpp.pre]") of a [*va-opt-replacement*](#nt:va-opt-replacement "15.7.2 Argument substitution [cpp.subst]") shall not contain __VA_OPT__[.](#cpp.subst-4.sentence-2)
|
||
|
||
If the [*pp-tokens*](cpp.pre#nt:pp-tokens "15.1 Preamble [cpp.pre]") would be ill-formed
|
||
as the replacement list of the current function-like macro,
|
||
the program is ill-formed[.](#cpp.subst-4.sentence-3)
|
||
|
||
A [*va-opt-replacement*](#nt:va-opt-replacement "15.7.2 Argument substitution [cpp.subst]") is treated as if it were a parameter,
|
||
and the preprocessing token sequence for the corresponding
|
||
argument is defined as follows[.](#cpp.subst-4.sentence-4)
|
||
|
||
If the substitution of __VA_ARGS__ as neither an operand
|
||
of # nor ## consists of no preprocessing tokens,
|
||
the argument consists of
|
||
a single placemarker preprocessing token ([[cpp.concat]](#cpp.concat "15.7.4 The ## operator"), [[cpp.rescan]](#cpp.rescan "15.7.5 Rescanning and further replacement"))[.](#cpp.subst-4.sentence-5)
|
||
|
||
Otherwise, the argument consists of
|
||
the results of the expansion of the contained [*pp-tokens*](cpp.pre#nt:pp-tokens "15.1 Preamble [cpp.pre]") as the replacement list of the current function-like macro
|
||
before removal of placemarker tokens, rescanning, and further replacement[.](#cpp.subst-4.sentence-6)
|
||
|
||
[*Note [1](#cpp.subst-note-1)*:
|
||
|
||
The placemarker tokens are removed before stringization ([[cpp.stringize]](#cpp.stringize "15.7.3 The # operator")),
|
||
and can be removed by rescanning and further replacement ([[cpp.rescan]](#cpp.rescan "15.7.5 Rescanning and further replacement"))[.](#cpp.subst-4.sentence-7)
|
||
|
||
â *end note*]
|
||
|
||
[*Example [3](#cpp.subst-example-3)*: #define F(...) f(0 __VA_OPT__(,) __VA_ARGS__)#define G(X, ...) f(0, X __VA_OPT__(,) __VA_ARGS__)#define SDEF(sname, ...) S sname __VA_OPT__(= { __VA_ARGS__ })#define EMP
|
||
|
||
F(a, b, c) // replaced by f(0, a, b, c) F() // replaced by f(0) F(EMP) // replaced by f(0) G(a, b, c) // replaced by f(0, a, b, c) G(a, ) // replaced by f(0, a) G(a) // replaced by f(0, a) SDEF(foo); // replaced by S foo; SDEF(bar, 1, 2); // replaced by S bar = { 1, 2 };#define H1(X, ...) X __VA_OPT__(##) __VA_ARGS__ // error: ## may not appear at// the beginning of a replacement list ([[cpp.concat]](#cpp.concat "15.7.4 The ## operator"))#define H2(X, Y, ...) __VA_OPT__(X ## Y,) __VA_ARGS__
|
||
H2(a, b, c, d) // replaced by ab, c, d#define H3(X, ...) #__VA_OPT__(X##X X##X) H3(, 0) // replaced by ""#define H4(X, ...) __VA_OPT__(a X ## X) ## b
|
||
H4(, 1) // replaced by a b#define H5A(...) __VA_OPT__()/**/__VA_OPT__()#define H5B(X) a ## X ## b#define H5C(X) H5B(X) H5C(H5A()) // replaced by ab â *end example*]
|
||
|
||
### [15.7.3](#cpp.stringize) The # operator [[cpp.stringize]](cpp.stringize)
|
||
|
||
[1](#cpp.stringize-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1795)
|
||
|
||
Each# preprocessing token in the replacement list for a function-like
|
||
macro shall be followed by a parameter as the next preprocessing
|
||
token in the replacement list[.](#cpp.stringize-1.sentence-1)
|
||
|
||
[2](#cpp.stringize-2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1802)
|
||
|
||
A [*character string literal*](#def:character_string_literal "15.7.3 The # operator [cpp.stringize]") is a [*string-literal*](lex.string#nt:string-literal "5.13.5 String literals [lex.string]") with no prefix[.](#cpp.stringize-2.sentence-1)
|
||
|
||
If, in the replacement list, a parameter is immediately
|
||
preceded by a# preprocessing token,
|
||
both are replaced by a single character string literal preprocessing token that
|
||
contains the spelling of the preprocessing token sequence for the
|
||
corresponding argument (excluding placemarker tokens)[.](#cpp.stringize-2.sentence-2)
|
||
|
||
Let the [*stringizing argument*](#def:stringizing_argument "15.7.3 The # operator [cpp.stringize]") be the preprocessing token sequence
|
||
for the corresponding argument with placemarker tokens removed[.](#cpp.stringize-2.sentence-3)
|
||
|
||
Each occurrence of whitespace between the stringizing argument's preprocessing
|
||
tokens becomes a single space character in the character string literal[.](#cpp.stringize-2.sentence-4)
|
||
|
||
Whitespace before the first preprocessing token and after the last
|
||
preprocessing token comprising the stringizing argument is deleted[.](#cpp.stringize-2.sentence-5)
|
||
|
||
Otherwise, the original spelling of each preprocessing token in the
|
||
stringizing argument is retained in the character string literal,
|
||
except for special handling for producing the spelling of[*header-name*](lex.header#nt:header-name "5.6 Header names [lex.header]")*s*,[*character-literal*](lex.ccon#nt:character-literal "5.13.3 Character literals [lex.ccon]")*s*,
|
||
and [*string-literal*](lex.string#nt:string-literal "5.13.5 String literals [lex.string]")*s* (including the delimiting U+0022 quotation mark ("))
|
||
contained within the preprocessing token:
|
||
a U+005c reverse solidus character (\)
|
||
is inserted before each U+0022 quotation mark andU+005c reverse solidus character of a[*header-name*](lex.header#nt:header-name "5.6 Header names [lex.header]"),[*character-literal*](lex.ccon#nt:character-literal "5.13.3 Character literals [lex.ccon]"),
|
||
or [*string-literal*](lex.string#nt:string-literal "5.13.5 String literals [lex.string]"),
|
||
and each new-line character is
|
||
replaced by the two-character sequence \n[.](#cpp.stringize-2.sentence-6)
|
||
|
||
If the replacement that results is not a valid character string literal,
|
||
the program is ill-formed[.](#cpp.stringize-2.sentence-7)
|
||
|
||
The character string literal corresponding to
|
||
an empty stringizing argument is ""[.](#cpp.stringize-2.sentence-8)
|
||
|
||
The order of evaluation of# and## operators is unspecified[.](#cpp.stringize-2.sentence-9)
|
||
|
||
### [15.7.4](#cpp.concat) The ## operator [[cpp.concat]](cpp.concat)
|
||
|
||
[1](#cpp.concat-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1847)
|
||
|
||
A## preprocessing token shall not occur at the beginning or
|
||
at the end of a replacement list for either form
|
||
of macro definition[.](#cpp.concat-1.sentence-1)
|
||
|
||
[2](#cpp.concat-2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1854)
|
||
|
||
If, in the replacement list of a function-like macro, a parameter is
|
||
immediately preceded or followed by a## preprocessing token, the parameter is replaced by the
|
||
corresponding argument's preprocessing token sequence; however, if an argument consists of no preprocessing tokens, the parameter is
|
||
replaced by a placemarker preprocessing token instead[.](#cpp.concat-2.sentence-1)[128](#footnote-128 "Placemarker preprocessing tokens do not appear in the syntax because they are temporary entities that exist only within translation phase 4.")
|
||
|
||
[3](#cpp.concat-3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1866)
|
||
|
||
For both object-like and function-like macro invocations, before the
|
||
replacement list is reexamined for more macro names to replace,
|
||
each instance of a## preprocessing token in the replacement list
|
||
(not from an argument) is deleted and the
|
||
preceding preprocessing token is concatenated
|
||
with the following preprocessing token[.](#cpp.concat-3.sentence-1)
|
||
|
||
Placemarker preprocessing tokens are handled specially: concatenation
|
||
of two placemarkers results in a single placemarker preprocessing token, and
|
||
concatenation of a placemarker with a non-placemarker preprocessing token results
|
||
in the non-placemarker preprocessing token[.](#cpp.concat-3.sentence-2)
|
||
|
||
[*Note [1](#cpp.concat-note-1)*:
|
||
|
||
Concatenation can form
|
||
a [*universal-character-name*](lex.universal.char#nt:universal-character-name "5.3.2 Universal character names [lex.universal.char]") ([[lex.charset]](lex.charset "5.3.1 Character sets"))[.](#cpp.concat-3.sentence-3)
|
||
|
||
â *end note*]
|
||
|
||
If the result is not a valid preprocessing token,
|
||
the program is ill-formed[.](#cpp.concat-3.sentence-4)
|
||
|
||
The resulting preprocessing token is available for further macro replacement[.](#cpp.concat-3.sentence-5)
|
||
|
||
The order of evaluation of## operators is unspecified[.](#cpp.concat-3.sentence-6)
|
||
|
||
[4](#cpp.concat-4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1890)
|
||
|
||
[*Example [1](#cpp.concat-example-1)*:
|
||
|
||
The sequence#define str(s) # s#define xstr(s) str(s)#define debug(s, t) printf("x" # s "= %d, x" # t "= %s", \
|
||
x ## s, x ## t)#define INCFILE(n) vers ## n#define glue(a, b) a ## b#define xglue(a, b) glue(a, b)#define HIGHLOW "hello"#define LOW LOW ", world" debug(1, 2);
|
||
fputs(str(strncmp("abc\0d", "abc", '\4') // this goes away== 0) str(: @\n), s);#include xstr(INCFILE(2).h) glue(HIGH, LOW);
|
||
xglue(HIGH, LOW) results inprintf("x" "1" "= %d, x" "2" "= %s", x1, x2);
|
||
fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0" ": @\n", s);#include "vers2.h" (*after macro replacement, before file access*)"hello";"hello" ", world" or, after concatenation of the character string literals,printf("x1= %d, x2= %s", x1, x2);
|
||
fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0: @\n", s);#include "vers2.h" (*after macro replacement, before file access*)"hello";"hello, world"
|
||
|
||
Space around the # and ## preprocessing tokens in the macro definition
|
||
is optional[.](#cpp.concat-4.sentence-2)
|
||
|
||
â *end example*]
|
||
|
||
[5](#cpp.concat-5)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1932)
|
||
|
||
[*Example [2](#cpp.concat-example-2)*:
|
||
|
||
In the following fragment:#define hash_hash # ## ##define mkstr(a) # a#define in_between(a) mkstr(a)#define join(c, d) in_between(c hash_hash d)char p[] = join(x, y); // equivalent to char p[] = "x ## y";
|
||
|
||
The expansion produces, at various stages:join(x, y) in_between(x hash_hash y) in_between(x ## y) mkstr(x ## y)"x ## y"
|
||
|
||
In other words, expanding hash_hash produces a new preprocessing token,
|
||
consisting of two adjacent sharp signs, but this new preprocessing token is not the## operator[.](#cpp.concat-5.sentence-3)
|
||
|
||
â *end example*]
|
||
|
||
[6](#cpp.concat-6)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1957)
|
||
|
||
[*Example [3](#cpp.concat-example-3)*:
|
||
|
||
To illustrate the rules for placemarker preprocessing tokens, the sequence#define t(x,y,z) x ## y ## zint j[] = { t(1,2,3), t(,4,5), t(6,,7), t(8,9,),
|
||
t(10,,), t(,11,), t(,,12), t(,,) }; results inint j[] = { 123, 45, 67, 89, 10, 11, 12, };
|
||
|
||
â *end example*]
|
||
|
||
[128)](#footnote-128)[128)](#footnoteref-128)
|
||
|
||
Placemarker preprocessing tokens do not appear in the syntax
|
||
because they are temporary entities that exist only within translation phase 4[.](#footnote-128.sentence-1)
|
||
|
||
### [15.7.5](#cpp.rescan) Rescanning and further replacement [[cpp.rescan]](cpp.rescan)
|
||
|
||
[1](#cpp.rescan-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1976)
|
||
|
||
After all parameters in the replacement list have been substituted and # and ## processing has taken
|
||
place, all placemarker preprocessing tokens are removed[.](#cpp.rescan-1.sentence-1)
|
||
|
||
Then
|
||
the resulting preprocessing token sequence is rescanned, along with all
|
||
subsequent preprocessing tokens of the source file, for more macro names
|
||
to replace[.](#cpp.rescan-1.sentence-2)
|
||
|
||
[2](#cpp.rescan-2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L1983)
|
||
|
||
[*Example [1](#cpp.rescan-example-1)*:
|
||
|
||
The sequence#define x 3#define f(a) f(x * (a))#undef x#define x 2#define g f#define z z[0]#define h g(~#define m(a) a(w)#define w 0,1#define t(a) a#define p() int#define q(x) x#define r(x,y) x ## y#define str(x) # x
|
||
|
||
f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);
|
||
g(x+(3,4)-w) | h 5) & m (f)^m(m);
|
||
p() i[q()] = { q(1), r(2,3), r(4,), r(,5), r(,) };char c[2][6] = { str(hello), str() }; results inf(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1);
|
||
f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1);int i[] = { 1, 23, 4, 5, };char c[2][6] = { "hello", "" };
|
||
|
||
â *end example*]
|
||
|
||
[3](#cpp.rescan-3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L2017)
|
||
|
||
If the name of the macro being replaced is found during this scan of
|
||
the replacement list
|
||
(not including the rest of the source file's preprocessing tokens),
|
||
it is not replaced[.](#cpp.rescan-3.sentence-1)
|
||
|
||
Furthermore,
|
||
if any nested replacements encounter the name of the macro being replaced,
|
||
it is not replaced[.](#cpp.rescan-3.sentence-2)
|
||
|
||
These nonreplaced macro name preprocessing tokens are no longer available
|
||
for further replacement even if they are later (re)examined in contexts
|
||
in which that macro name preprocessing token would otherwise have been
|
||
replaced[.](#cpp.rescan-3.sentence-3)
|
||
|
||
[4](#cpp.rescan-4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L2030)
|
||
|
||
The resulting completely macro-replaced preprocessing token sequence
|
||
is not processed as a preprocessing directive even if it resembles one,
|
||
but all pragma unary operator expressions within it are then processed as
|
||
specified in [[cpp.pragma.op]](cpp.pragma.op "15.13 Pragma operator") below[.](#cpp.rescan-4.sentence-1)
|
||
|
||
### [15.7.6](#cpp.scope) Scope of macro definitions [[cpp.scope]](cpp.scope)
|
||
|
||
[1](#cpp.scope-1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L2040)
|
||
|
||
A macro definition lasts
|
||
(independent of block structure)
|
||
until a corresponding#undef directive is encountered or
|
||
(if none is encountered)
|
||
until the end of the translation unit[.](#cpp.scope-1.sentence-1)
|
||
|
||
Macro definitions have no significance after translation phase 4[.](#cpp.scope-1.sentence-2)
|
||
|
||
[2](#cpp.scope-2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/preprocessor.tex#L2050)
|
||
|
||
A preprocessing directive of the form
|
||
|
||
# undef [*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]") [*new-line*](cpp.pre#nt:new-line "15.1 Preamble [cpp.pre]")
|
||
|
||
causes the specified identifier no longer to be defined as a macro name[.](#cpp.scope-2.sentence-1)
|
||
|
||
It is ignored if the specified identifier is not currently defined as
|
||
a macro name[.](#cpp.scope-2.sentence-2)
|