- tmp/tmpfeov8fvl/{from.md → to.md} +286 -203
tmp/tmpfeov8fvl/{from.md → to.md}
RENAMED
|
@@ -12,24 +12,48 @@ Likewise, an identifier currently defined as a function-like macro (see
|
|
| 12 |
below) may be redefined by another `#define` preprocessing directive
|
| 13 |
provided that the second definition is a function-like macro definition
|
| 14 |
that has the same number and spelling of parameters, and the two
|
| 15 |
replacement lists are identical, otherwise the program is ill-formed.
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
There shall be white-space between the identifier and the replacement
|
| 18 |
list in the definition of an object-like macro.
|
| 19 |
|
| 20 |
If the *identifier-list* in the macro definition does not end with an
|
| 21 |
ellipsis, the number of arguments (including those arguments consisting
|
| 22 |
of no preprocessing tokens) in an invocation of a function-like macro
|
| 23 |
shall equal the number of parameters in the macro definition. Otherwise,
|
| 24 |
-
there shall be
|
| 25 |
parameters in the macro definition (excluding the `...`). There shall
|
| 26 |
exist a `)` preprocessing token that terminates the invocation.
|
| 27 |
|
| 28 |
-
The
|
| 29 |
-
of a function-like macro that uses the ellipsis
|
| 30 |
-
parameters.
|
| 31 |
|
| 32 |
A parameter identifier in a function-like macro shall be uniquely
|
| 33 |
declared within its scope.
|
| 34 |
|
| 35 |
The identifier immediately following the `define` is called the *macro
|
|
@@ -52,10 +76,22 @@ defines an *object-like macro* that causes each subsequent instance of
|
|
| 52 |
the macro name[^5] to be replaced by the replacement list of
|
| 53 |
preprocessing tokens that constitute the remainder of the directive.[^6]
|
| 54 |
The replacement list is then rescanned for more macro names as specified
|
| 55 |
below.
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
A preprocessing directive of the form
|
| 58 |
|
| 59 |
``` bnf
|
| 60 |
'# define' identifier lparen identifier-listₒₚₜ ')' replacement-list new-line
|
| 61 |
'# define' identifier lparen '...' ')' replacement-list new-line
|
|
@@ -82,56 +118,184 @@ macro. The individual arguments within the list are separated by comma
|
|
| 82 |
preprocessing tokens, but comma preprocessing tokens between matching
|
| 83 |
inner parentheses do not separate arguments. If there are sequences of
|
| 84 |
preprocessing tokens within the list of arguments that would otherwise
|
| 85 |
act as preprocessing directives,[^7] the behavior is undefined.
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
If there is a `...` immediately preceding the `)` in the function-like
|
| 88 |
-
macro definition, then the trailing arguments, including any
|
| 89 |
-
comma preprocessing tokens, are merged to form a single item:
|
| 90 |
-
*variable arguments*. The number of arguments so combined is such
|
| 91 |
-
following merger, the number of arguments is
|
| 92 |
-
|
|
|
|
| 93 |
|
| 94 |
### Argument substitution <a id="cpp.subst">[[cpp.subst]]</a>
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
After the arguments for the invocation of a function-like macro have
|
| 97 |
-
been identified, argument substitution takes place.
|
| 98 |
-
replacement list
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
An identifier `__VA_ARGS__` that occurs in the replacement list shall be
|
| 106 |
treated as if it were a parameter, and the variable arguments shall form
|
| 107 |
the preprocessing tokens used to replace it.
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
### The `#` operator <a id="cpp.stringize">[[cpp.stringize]]</a>
|
| 110 |
|
| 111 |
Each `#` preprocessing token in the replacement list for a function-like
|
| 112 |
macro shall be followed by a parameter as the next preprocessing token
|
| 113 |
in the replacement list.
|
| 114 |
|
| 115 |
A *character string literal* is a *string-literal* with no prefix. If,
|
| 116 |
in the replacement list, a parameter is immediately preceded by a `#`
|
| 117 |
preprocessing token, both are replaced by a single character string
|
| 118 |
literal preprocessing token that contains the spelling of the
|
| 119 |
-
preprocessing token sequence for the corresponding argument
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
preprocessing
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
character literal
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
### The `##` operator <a id="cpp.concat">[[cpp.concat]]</a>
|
| 135 |
|
| 136 |
A `##` preprocessing token shall not occur at the beginning or at the
|
| 137 |
end of a replacement list for either form of macro definition.
|
|
@@ -156,10 +320,58 @@ is not a valid preprocessing token, the behavior is undefined. The
|
|
| 156 |
resulting token is available for further macro replacement. The order of
|
| 157 |
evaluation of `##` operators is unspecified.
|
| 158 |
|
| 159 |
[*Example 1*:
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
In the following fragment:
|
| 162 |
|
| 163 |
``` cpp
|
| 164 |
#define hash_hash # ## #
|
| 165 |
#define mkstr(a) # a
|
|
@@ -182,83 +394,41 @@ In other words, expanding `hash_hash` produces a new token, consisting
|
|
| 182 |
of two adjacent sharp signs, but this new token is not the `##`
|
| 183 |
operator.
|
| 184 |
|
| 185 |
— *end example*]
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
### Rescanning and further replacement <a id="cpp.rescan">[[cpp.rescan]]</a>
|
| 188 |
|
| 189 |
After all parameters in the replacement list have been substituted and
|
| 190 |
`#` and `##` processing has taken place, all placemarker preprocessing
|
| 191 |
tokens are removed. Then the resulting preprocessing token sequence is
|
| 192 |
rescanned, along with all subsequent preprocessing tokens of the source
|
| 193 |
file, for more macro names to replace.
|
| 194 |
|
| 195 |
-
If the name of the macro being replaced is found during this scan of the
|
| 196 |
-
replacement list (not including the rest of the source file’s
|
| 197 |
-
preprocessing tokens), it is not replaced. Furthermore, if any nested
|
| 198 |
-
replacements encounter the name of the macro being replaced, it is not
|
| 199 |
-
replaced. These nonreplaced macro name preprocessing tokens are no
|
| 200 |
-
longer available for further replacement even if they are later
|
| 201 |
-
(re)examined in contexts in which that macro name preprocessing token
|
| 202 |
-
would otherwise have been replaced.
|
| 203 |
-
|
| 204 |
-
The resulting completely macro-replaced preprocessing token sequence is
|
| 205 |
-
not processed as a preprocessing directive even if it resembles one, but
|
| 206 |
-
all pragma unary operator expressions within it are then processed as
|
| 207 |
-
specified in [[cpp.pragma.op]] below.
|
| 208 |
-
|
| 209 |
-
### Scope of macro definitions <a id="cpp.scope">[[cpp.scope]]</a>
|
| 210 |
-
|
| 211 |
-
A macro definition lasts (independent of block structure) until a
|
| 212 |
-
corresponding `#undef` directive is encountered or (if none is
|
| 213 |
-
encountered) until the end of the translation unit. Macro definitions
|
| 214 |
-
have no significance after translation phase 4.
|
| 215 |
-
|
| 216 |
-
A preprocessing directive of the form
|
| 217 |
-
|
| 218 |
-
``` bnf
|
| 219 |
-
'# undef' identifier new-line
|
| 220 |
-
```
|
| 221 |
-
|
| 222 |
-
causes the specified identifier no longer to be defined as a macro name.
|
| 223 |
-
It is ignored if the specified identifier is not currently defined as a
|
| 224 |
-
macro name.
|
| 225 |
-
|
| 226 |
[*Example 1*:
|
| 227 |
|
| 228 |
-
The
|
| 229 |
-
in
|
| 230 |
-
|
| 231 |
-
``` cpp
|
| 232 |
-
#define TABSIZE 100
|
| 233 |
-
int table[TABSIZE];
|
| 234 |
-
```
|
| 235 |
-
|
| 236 |
-
— *end example*]
|
| 237 |
-
|
| 238 |
-
[*Example 2*:
|
| 239 |
-
|
| 240 |
-
The following defines a function-like macro whose value is the maximum
|
| 241 |
-
of its arguments. It has the advantages of working for any compatible
|
| 242 |
-
types of the arguments and of generating in-line code without the
|
| 243 |
-
overhead of function calling. It has the disadvantages of evaluating one
|
| 244 |
-
or the other of its arguments a second time (including side effects) and
|
| 245 |
-
generating more code than a function if invoked several times. It also
|
| 246 |
-
cannot have its address taken, as it has none.
|
| 247 |
-
|
| 248 |
-
``` cpp
|
| 249 |
-
#define max(a, b) ((a) > (b) ? (a) : (b))
|
| 250 |
-
```
|
| 251 |
-
|
| 252 |
-
The parentheses ensure that the arguments and the resulting expression
|
| 253 |
-
are bound properly.
|
| 254 |
-
|
| 255 |
-
— *end example*]
|
| 256 |
-
|
| 257 |
-
[*Example 3*:
|
| 258 |
-
|
| 259 |
-
To illustrate the rules for redefinition and reexamination, the sequence
|
| 260 |
|
| 261 |
``` cpp
|
| 262 |
#define x 3
|
| 263 |
#define f(a) f(x * (a))
|
| 264 |
#undef x
|
|
@@ -290,123 +460,36 @@ int i[] = { 1, 23, 4, 5, };
|
|
| 290 |
char c[2][6] = { "hello", "" };
|
| 291 |
```
|
| 292 |
|
| 293 |
— *end example*]
|
| 294 |
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
-
|
| 298 |
-
|
|
|
|
|
|
|
| 299 |
|
| 300 |
-
|
| 301 |
-
#define str(s) # s
|
| 302 |
-
#define xstr(s) str(s)
|
| 303 |
-
#define debug(s, t) printf("x" # s "= %d, x" # t "= %s", \
|
| 304 |
-
x ## s, x ## t)
|
| 305 |
-
#define INCFILE(n) vers ## n
|
| 306 |
-
#define glue(a, b) a ## b
|
| 307 |
-
#define xglue(a, b) glue(a, b)
|
| 308 |
-
#define HIGHLOW "hello"
|
| 309 |
-
#define LOW LOW ", world"
|
| 310 |
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
glue(HIGH, LOW);
|
| 316 |
-
xglue(HIGH, LOW)
|
| 317 |
-
```
|
| 318 |
-
|
| 319 |
-
results in
|
| 320 |
-
|
| 321 |
-
``` cpp
|
| 322 |
-
printf("x" "1" "= %d, x" "2" "= %s", x1, x2);
|
| 323 |
-
fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0" ": \@n", s);
|
| 324 |
-
#include "vers2.h" (after macro replacement, before file access)
|
| 325 |
-
"hello";
|
| 326 |
-
"hello" ", world"
|
| 327 |
-
```
|
| 328 |
-
|
| 329 |
-
or, after concatenation of the character string literals,
|
| 330 |
-
|
| 331 |
-
``` cpp
|
| 332 |
-
printf("x1= %d, x2= %s", x1, x2);
|
| 333 |
-
fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0: \@n", s);
|
| 334 |
-
#include "vers2.h" (after macro replacement, before file access)
|
| 335 |
-
"hello";
|
| 336 |
-
"hello, world"
|
| 337 |
-
```
|
| 338 |
-
|
| 339 |
-
Space around the `#` and `##` tokens in the macro definition is
|
| 340 |
-
optional.
|
| 341 |
-
|
| 342 |
-
— *end example*]
|
| 343 |
-
|
| 344 |
-
[*Example 5*:
|
| 345 |
-
|
| 346 |
-
To illustrate the rules for placemarker preprocessing tokens, the
|
| 347 |
-
sequence
|
| 348 |
-
|
| 349 |
-
``` cpp
|
| 350 |
-
#define t(x,y,z) x ## y ## z
|
| 351 |
-
int j[] = { t(1,2,3), t(,4,5), t(6,,7), t(8,9,),
|
| 352 |
-
t(10,,), t(,11,), t(,,12), t(,,) };
|
| 353 |
-
```
|
| 354 |
-
|
| 355 |
-
results in
|
| 356 |
-
|
| 357 |
-
``` cpp
|
| 358 |
-
int j[] = { 123, 45, 67, 89,
|
| 359 |
-
10, 11, 12, };
|
| 360 |
-
```
|
| 361 |
-
|
| 362 |
-
— *end example*]
|
| 363 |
-
|
| 364 |
-
[*Example 6*:
|
| 365 |
-
|
| 366 |
-
To demonstrate the redefinition rules, the following sequence is valid.
|
| 367 |
-
|
| 368 |
-
``` cpp
|
| 369 |
-
#define OBJ_LIKE (1-1)
|
| 370 |
-
#define OBJ_LIKE /* white space */ (1-1) /* other */
|
| 371 |
-
#define FUNC_LIKE(a) ( a )
|
| 372 |
-
#define FUNC_LIKE( a )( /* note the white space */ \
|
| 373 |
-
a /* other stuff on this line
|
| 374 |
-
*/ )
|
| 375 |
-
```
|
| 376 |
-
|
| 377 |
-
But the following redefinitions are invalid:
|
| 378 |
-
|
| 379 |
-
``` cpp
|
| 380 |
-
#define OBJ_LIKE (0) // different token sequence
|
| 381 |
-
#define OBJ_LIKE (1 - 1) // different white space
|
| 382 |
-
#define FUNC_LIKE(b) ( a ) // different parameter usage
|
| 383 |
-
#define FUNC_LIKE(b) ( b ) // different parameter spelling
|
| 384 |
-
```
|
| 385 |
-
|
| 386 |
-
— *end example*]
|
| 387 |
-
|
| 388 |
-
[*Example 7*:
|
| 389 |
-
|
| 390 |
-
Finally, to show the variable argument list macro facilities:
|
| 391 |
-
|
| 392 |
-
``` cpp
|
| 393 |
-
#define debug(...) fprintf(stderr, __VA_ARGS__)
|
| 394 |
-
#define showlist(...) puts(#__VA_ARGS__)
|
| 395 |
-
#define report(test, ...) ((test) ? puts(#test) : printf(__VA_ARGS__))
|
| 396 |
-
debug("Flag");
|
| 397 |
-
debug("X = %d\n", x);
|
| 398 |
-
showlist(The first, second, and third items.);
|
| 399 |
-
report(x>y, "x is %d but y is %d", x, y);
|
| 400 |
-
```
|
| 401 |
|
| 402 |
-
|
| 403 |
|
| 404 |
-
```
|
| 405 |
-
|
| 406 |
-
fprintf(stderr, "X = %d\n", x);
|
| 407 |
-
puts("The first, second, and third items.");
|
| 408 |
-
((x>y) ? puts("x>y") : printf("x is %d but y is %d", x, y));
|
| 409 |
```
|
| 410 |
|
| 411 |
-
|
|
|
|
|
|
|
| 412 |
|
|
|
|
| 12 |
below) may be redefined by another `#define` preprocessing directive
|
| 13 |
provided that the second definition is a function-like macro definition
|
| 14 |
that has the same number and spelling of parameters, and the two
|
| 15 |
replacement lists are identical, otherwise the program is ill-formed.
|
| 16 |
|
| 17 |
+
[*Example 1*:
|
| 18 |
+
|
| 19 |
+
The following sequence is valid:
|
| 20 |
+
|
| 21 |
+
``` cpp
|
| 22 |
+
#define OBJ_LIKE (1-1)
|
| 23 |
+
#define OBJ_LIKE /* white space */ (1-1) /* other */
|
| 24 |
+
#define FUNC_LIKE(a) ( a )
|
| 25 |
+
#define FUNC_LIKE( a )( /* note the white space */ \
|
| 26 |
+
a /* other stuff on this line
|
| 27 |
+
*/ )
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
But the following redefinitions are invalid:
|
| 31 |
+
|
| 32 |
+
``` cpp
|
| 33 |
+
#define OBJ_LIKE (0) // different token sequence
|
| 34 |
+
#define OBJ_LIKE (1 - 1) // different white space
|
| 35 |
+
#define FUNC_LIKE(b) ( a ) // different parameter usage
|
| 36 |
+
#define FUNC_LIKE(b) ( b ) // different parameter spelling
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
— *end example*]
|
| 40 |
+
|
| 41 |
There shall be white-space between the identifier and the replacement
|
| 42 |
list in the definition of an object-like macro.
|
| 43 |
|
| 44 |
If the *identifier-list* in the macro definition does not end with an
|
| 45 |
ellipsis, the number of arguments (including those arguments consisting
|
| 46 |
of no preprocessing tokens) in an invocation of a function-like macro
|
| 47 |
shall equal the number of parameters in the macro definition. Otherwise,
|
| 48 |
+
there shall be at least as many arguments in the invocation as there are
|
| 49 |
parameters in the macro definition (excluding the `...`). There shall
|
| 50 |
exist a `)` preprocessing token that terminates the invocation.
|
| 51 |
|
| 52 |
+
The identifiers `__VA_ARGS__` and `__VA_OPT__` shall occur only in the
|
| 53 |
+
*replacement-list* of a function-like macro that uses the ellipsis
|
| 54 |
+
notation in the parameters.
|
| 55 |
|
| 56 |
A parameter identifier in a function-like macro shall be uniquely
|
| 57 |
declared within its scope.
|
| 58 |
|
| 59 |
The identifier immediately following the `define` is called the *macro
|
|
|
|
| 76 |
the macro name[^5] to be replaced by the replacement list of
|
| 77 |
preprocessing tokens that constitute the remainder of the directive.[^6]
|
| 78 |
The replacement list is then rescanned for more macro names as specified
|
| 79 |
below.
|
| 80 |
|
| 81 |
+
[*Example 2*:
|
| 82 |
+
|
| 83 |
+
The simplest use of this facility is to define a “manifest constant”, as
|
| 84 |
+
in
|
| 85 |
+
|
| 86 |
+
``` cpp
|
| 87 |
+
#define TABSIZE 100
|
| 88 |
+
int table[TABSIZE];
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
— *end example*]
|
| 92 |
+
|
| 93 |
A preprocessing directive of the form
|
| 94 |
|
| 95 |
``` bnf
|
| 96 |
'# define' identifier lparen identifier-listₒₚₜ ')' replacement-list new-line
|
| 97 |
'# define' identifier lparen '...' ')' replacement-list new-line
|
|
|
|
| 118 |
preprocessing tokens, but comma preprocessing tokens between matching
|
| 119 |
inner parentheses do not separate arguments. If there are sequences of
|
| 120 |
preprocessing tokens within the list of arguments that would otherwise
|
| 121 |
act as preprocessing directives,[^7] the behavior is undefined.
|
| 122 |
|
| 123 |
+
[*Example 3*:
|
| 124 |
+
|
| 125 |
+
The following defines a function-like macro whose value is the maximum
|
| 126 |
+
of its arguments. It has the disadvantages of evaluating one or the
|
| 127 |
+
other of its arguments a second time (including side effects) and
|
| 128 |
+
generating more code than a function if invoked several times. It also
|
| 129 |
+
cannot have its address taken, as it has none.
|
| 130 |
+
|
| 131 |
+
``` cpp
|
| 132 |
+
#define max(a, b) ((a) > (b) ? (a) : (b))
|
| 133 |
+
```
|
| 134 |
+
|
| 135 |
+
The parentheses ensure that the arguments and the resulting expression
|
| 136 |
+
are bound properly.
|
| 137 |
+
|
| 138 |
+
— *end example*]
|
| 139 |
+
|
| 140 |
If there is a `...` immediately preceding the `)` in the function-like
|
| 141 |
+
macro definition, then the trailing arguments (if any), including any
|
| 142 |
+
separating comma preprocessing tokens, are merged to form a single item:
|
| 143 |
+
the *variable arguments*. The number of arguments so combined is such
|
| 144 |
+
that, following merger, the number of arguments is either equal to or
|
| 145 |
+
one more than the number of parameters in the macro definition
|
| 146 |
+
(excluding the `...`).
|
| 147 |
|
| 148 |
### Argument substitution <a id="cpp.subst">[[cpp.subst]]</a>
|
| 149 |
|
| 150 |
+
``` bnf
|
| 151 |
+
va-opt-replacement:
|
| 152 |
+
'__VA_OPT__ (' pp-tokensₒₚₜ ')'
|
| 153 |
+
```
|
| 154 |
+
|
| 155 |
After the arguments for the invocation of a function-like macro have
|
| 156 |
+
been identified, argument substitution takes place. For each parameter
|
| 157 |
+
in the replacement list that is neither preceded by a `#` or `##`
|
| 158 |
+
preprocessing token nor followed by a `##` preprocessing token, the
|
| 159 |
+
preprocessing tokens naming the parameter are replaced by a token
|
| 160 |
+
sequence determined as follows:
|
| 161 |
+
|
| 162 |
+
- If the parameter is of the form *va-opt-replacement*, the replacement
|
| 163 |
+
preprocessing tokens are the preprocessing token sequence for the
|
| 164 |
+
corresponding argument, as specified below.
|
| 165 |
+
- Otherwise, the replacement preprocessing tokens are the preprocessing
|
| 166 |
+
tokens of corresponding argument after all macros contained therein
|
| 167 |
+
have been expanded. The argument’s preprocessing tokens are completely
|
| 168 |
+
macro replaced before being substituted as if they formed the rest of
|
| 169 |
+
the preprocessing file with no other preprocessing tokens being
|
| 170 |
+
available.
|
| 171 |
+
|
| 172 |
+
[*Example 1*:
|
| 173 |
+
|
| 174 |
+
``` cpp
|
| 175 |
+
#define LPAREN() (
|
| 176 |
+
#define G(Q) 42
|
| 177 |
+
#define F(R, X, ...) __VA_OPT__(G R X) )
|
| 178 |
+
int x = F(LPAREN(), 0, <:-); // replaced by int x = 42;
|
| 179 |
+
```
|
| 180 |
+
|
| 181 |
+
— *end example*]
|
| 182 |
|
| 183 |
An identifier `__VA_ARGS__` that occurs in the replacement list shall be
|
| 184 |
treated as if it were a parameter, and the variable arguments shall form
|
| 185 |
the preprocessing tokens used to replace it.
|
| 186 |
|
| 187 |
+
[*Example 2*:
|
| 188 |
+
|
| 189 |
+
``` cpp
|
| 190 |
+
#define debug(...) fprintf(stderr, __VA_ARGS__)
|
| 191 |
+
#define showlist(...) puts(#__VA_ARGS__)
|
| 192 |
+
#define report(test, ...) ((test) ? puts(#test) : printf(__VA_ARGS__))
|
| 193 |
+
debug("Flag");
|
| 194 |
+
debug("X = %d\n", x);
|
| 195 |
+
showlist(The first, second, and third items.);
|
| 196 |
+
report(x>y, "x is %d but y is %d", x, y);
|
| 197 |
+
```
|
| 198 |
+
|
| 199 |
+
results in
|
| 200 |
+
|
| 201 |
+
``` cpp
|
| 202 |
+
fprintf(stderr, "Flag");
|
| 203 |
+
fprintf(stderr, "X = %d\n", x);
|
| 204 |
+
puts("The first, second, and third items.");
|
| 205 |
+
((x>y) ? puts("x>y") : printf("x is %d but y is %d", x, y));
|
| 206 |
+
```
|
| 207 |
+
|
| 208 |
+
— *end example*]
|
| 209 |
+
|
| 210 |
+
The identifier `__VA_OPT__` shall always occur as part of the
|
| 211 |
+
preprocessing token sequence *va-opt-replacement*; its closing `)` is
|
| 212 |
+
determined by skipping intervening pairs of matching left and right
|
| 213 |
+
parentheses in its *pp-tokens*. The *pp-tokens* of a
|
| 214 |
+
*va-opt-replacement* shall not contain `__VA_OPT__`. If the *pp-tokens*
|
| 215 |
+
would be ill-formed as the replacement list of the current function-like
|
| 216 |
+
macro, the program is ill-formed. A *va-opt-replacement* is treated as
|
| 217 |
+
if it were a parameter, and the preprocessing token sequence for the
|
| 218 |
+
corresponding argument is defined as follows. If the substitution of
|
| 219 |
+
`__VA_ARGS__` as neither an operand of `#` nor `##` consists of no
|
| 220 |
+
preprocessing tokens, the argument consists of a single placemarker
|
| 221 |
+
preprocessing token ([[cpp.concat]], [[cpp.rescan]]). Otherwise, the
|
| 222 |
+
argument consists of the results of the expansion of the contained
|
| 223 |
+
*pp-tokens* as the replacement list of the current function-like macro
|
| 224 |
+
before removal of placemarker tokens, rescanning, and further
|
| 225 |
+
replacement.
|
| 226 |
+
|
| 227 |
+
[*Note 1*: The placemarker tokens are removed before stringization
|
| 228 |
+
[[cpp.stringize]], and can be removed by rescanning and further
|
| 229 |
+
replacement [[cpp.rescan]]. — *end note*]
|
| 230 |
+
|
| 231 |
+
[*Example 3*:
|
| 232 |
+
|
| 233 |
+
``` cpp
|
| 234 |
+
#define F(...) f(0 __VA_OPT__(,) __VA_ARGS__)
|
| 235 |
+
#define G(X, ...) f(0, X __VA_OPT__(,) __VA_ARGS__)
|
| 236 |
+
#define SDEF(sname, ...) S sname __VA_OPT__(= { __VA_ARGS__ })
|
| 237 |
+
#define EMP
|
| 238 |
+
|
| 239 |
+
F(a, b, c) // replaced by f(0, a, b, c)
|
| 240 |
+
F() // replaced by f(0)
|
| 241 |
+
F(EMP) // replaced by f(0)
|
| 242 |
+
|
| 243 |
+
G(a, b, c) // replaced by f(0, a, b, c)
|
| 244 |
+
G(a, ) // replaced by f(0, a)
|
| 245 |
+
G(a) // replaced by f(0, a)
|
| 246 |
+
|
| 247 |
+
SDEF(foo); // replaced by S foo;
|
| 248 |
+
SDEF(bar, 1, 2); // replaced by S bar = { 1, 2 }
|
| 249 |
+
|
| 250 |
+
#define H1(X, ...) X __VA_OPT__(##) __VA_ARGS__ // error: ## may not appear at
|
| 251 |
+
// the beginning of a replacement list[cpp.concat]
|
| 252 |
+
|
| 253 |
+
#define H2(X, Y, ...) __VA_OPT__(X ## Y,) __VA_ARGS__
|
| 254 |
+
H2(a, b, c, d) // replaced by ab, c, d
|
| 255 |
+
|
| 256 |
+
#define H3(X, ...) #__VA_OPT__(X##X X##X)
|
| 257 |
+
H3(, 0) // replaced by ""
|
| 258 |
+
|
| 259 |
+
#define H4(X, ...) __VA_OPT__(a X ## X) ## b
|
| 260 |
+
H4(, 1) // replaced by a b
|
| 261 |
+
|
| 262 |
+
#define H5A(...) __VA_OPT__()/**/__VA_OPT__()
|
| 263 |
+
#define H5B(X) a ## X ## b
|
| 264 |
+
#define H5C(X) H5B(X)
|
| 265 |
+
H5C(H5A()) // replaced by ab
|
| 266 |
+
```
|
| 267 |
+
|
| 268 |
+
— *end example*]
|
| 269 |
+
|
| 270 |
### The `#` operator <a id="cpp.stringize">[[cpp.stringize]]</a>
|
| 271 |
|
| 272 |
Each `#` preprocessing token in the replacement list for a function-like
|
| 273 |
macro shall be followed by a parameter as the next preprocessing token
|
| 274 |
in the replacement list.
|
| 275 |
|
| 276 |
A *character string literal* is a *string-literal* with no prefix. If,
|
| 277 |
in the replacement list, a parameter is immediately preceded by a `#`
|
| 278 |
preprocessing token, both are replaced by a single character string
|
| 279 |
literal preprocessing token that contains the spelling of the
|
| 280 |
+
preprocessing token sequence for the corresponding argument (excluding
|
| 281 |
+
placemarker tokens). Let the *stringizing argument* be the preprocessing
|
| 282 |
+
token sequence for the corresponding argument with placemarker tokens
|
| 283 |
+
removed. Each occurrence of white space between the stringizing
|
| 284 |
+
argument’s preprocessing tokens becomes a single space character in the
|
| 285 |
+
character string literal. White space before the first preprocessing
|
| 286 |
+
token and after the last preprocessing token comprising the stringizing
|
| 287 |
+
argument is deleted. Otherwise, the original spelling of each
|
| 288 |
+
preprocessing token in the stringizing argument is retained in the
|
| 289 |
+
character string literal, except for special handling for producing the
|
| 290 |
+
spelling of *string-literal*s and *character-literal*s: a `\` character
|
| 291 |
+
is inserted before each `"` and `\` character of a *character-literal*
|
| 292 |
+
or *string-literal* (including the delimiting `"` characters). If the
|
| 293 |
+
replacement that results is not a valid character string literal, the
|
| 294 |
+
behavior is undefined. The character string literal corresponding to an
|
| 295 |
+
empty stringizing argument is `""`. The order of evaluation of `#` and
|
| 296 |
+
`##` operators is unspecified.
|
| 297 |
|
| 298 |
### The `##` operator <a id="cpp.concat">[[cpp.concat]]</a>
|
| 299 |
|
| 300 |
A `##` preprocessing token shall not occur at the beginning or at the
|
| 301 |
end of a replacement list for either form of macro definition.
|
|
|
|
| 320 |
resulting token is available for further macro replacement. The order of
|
| 321 |
evaluation of `##` operators is unspecified.
|
| 322 |
|
| 323 |
[*Example 1*:
|
| 324 |
|
| 325 |
+
The sequence
|
| 326 |
+
|
| 327 |
+
``` cpp
|
| 328 |
+
#define str(s) # s
|
| 329 |
+
#define xstr(s) str(s)
|
| 330 |
+
#define debug(s, t) printf("x" # s "= %d, x" # t "= %s", \
|
| 331 |
+
x ## s, x ## t)
|
| 332 |
+
#define INCFILE(n) vers ## n
|
| 333 |
+
#define glue(a, b) a ## b
|
| 334 |
+
#define xglue(a, b) glue(a, b)
|
| 335 |
+
#define HIGHLOW "hello"
|
| 336 |
+
#define LOW LOW ", world"
|
| 337 |
+
|
| 338 |
+
debug(1, 2);
|
| 339 |
+
fputs(str(strncmp("abc\0d", "abc", '\4') // this goes away
|
| 340 |
+
== 0) str(: \@n), s);
|
| 341 |
+
#include xstr(INCFILE(2).h)
|
| 342 |
+
glue(HIGH, LOW);
|
| 343 |
+
xglue(HIGH, LOW)
|
| 344 |
+
```
|
| 345 |
+
|
| 346 |
+
results in
|
| 347 |
+
|
| 348 |
+
``` cpp
|
| 349 |
+
printf("x" "1" "= %d, x" "2" "= %s", x1, x2);
|
| 350 |
+
fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0" ": \@n", s);
|
| 351 |
+
#include "vers2.h" (after macro replacement, before file access)
|
| 352 |
+
"hello";
|
| 353 |
+
"hello" ", world"
|
| 354 |
+
```
|
| 355 |
+
|
| 356 |
+
or, after concatenation of the character string literals,
|
| 357 |
+
|
| 358 |
+
``` cpp
|
| 359 |
+
printf("x1= %d, x2= %s", x1, x2);
|
| 360 |
+
fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0: \@n", s);
|
| 361 |
+
#include "vers2.h" (after macro replacement, before file access)
|
| 362 |
+
"hello";
|
| 363 |
+
"hello, world"
|
| 364 |
+
```
|
| 365 |
+
|
| 366 |
+
Space around the `#` and `##` tokens in the macro definition is
|
| 367 |
+
optional.
|
| 368 |
+
|
| 369 |
+
— *end example*]
|
| 370 |
+
|
| 371 |
+
[*Example 2*:
|
| 372 |
+
|
| 373 |
In the following fragment:
|
| 374 |
|
| 375 |
``` cpp
|
| 376 |
#define hash_hash # ## #
|
| 377 |
#define mkstr(a) # a
|
|
|
|
| 394 |
of two adjacent sharp signs, but this new token is not the `##`
|
| 395 |
operator.
|
| 396 |
|
| 397 |
— *end example*]
|
| 398 |
|
| 399 |
+
[*Example 3*:
|
| 400 |
+
|
| 401 |
+
To illustrate the rules for placemarker preprocessing tokens, the
|
| 402 |
+
sequence
|
| 403 |
+
|
| 404 |
+
``` cpp
|
| 405 |
+
#define t(x,y,z) x ## y ## z
|
| 406 |
+
int j[] = { t(1,2,3), t(,4,5), t(6,,7), t(8,9,),
|
| 407 |
+
t(10,,), t(,11,), t(,,12), t(,,) };
|
| 408 |
+
```
|
| 409 |
+
|
| 410 |
+
results in
|
| 411 |
+
|
| 412 |
+
``` cpp
|
| 413 |
+
int j[] = { 123, 45, 67, 89,
|
| 414 |
+
10, 11, 12, };
|
| 415 |
+
```
|
| 416 |
+
|
| 417 |
+
— *end example*]
|
| 418 |
+
|
| 419 |
### Rescanning and further replacement <a id="cpp.rescan">[[cpp.rescan]]</a>
|
| 420 |
|
| 421 |
After all parameters in the replacement list have been substituted and
|
| 422 |
`#` and `##` processing has taken place, all placemarker preprocessing
|
| 423 |
tokens are removed. Then the resulting preprocessing token sequence is
|
| 424 |
rescanned, along with all subsequent preprocessing tokens of the source
|
| 425 |
file, for more macro names to replace.
|
| 426 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 427 |
[*Example 1*:
|
| 428 |
|
| 429 |
+
The sequence
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 430 |
|
| 431 |
``` cpp
|
| 432 |
#define x 3
|
| 433 |
#define f(a) f(x * (a))
|
| 434 |
#undef x
|
|
|
|
| 460 |
char c[2][6] = { "hello", "" };
|
| 461 |
```
|
| 462 |
|
| 463 |
— *end example*]
|
| 464 |
|
| 465 |
+
If the name of the macro being replaced is found during this scan of the
|
| 466 |
+
replacement list (not including the rest of the source file’s
|
| 467 |
+
preprocessing tokens), it is not replaced. Furthermore, if any nested
|
| 468 |
+
replacements encounter the name of the macro being replaced, it is not
|
| 469 |
+
replaced. These nonreplaced macro name preprocessing tokens are no
|
| 470 |
+
longer available for further replacement even if they are later
|
| 471 |
+
(re)examined in contexts in which that macro name preprocessing token
|
| 472 |
+
would otherwise have been replaced.
|
| 473 |
|
| 474 |
+
The resulting completely macro-replaced preprocessing token sequence is
|
| 475 |
+
not processed as a preprocessing directive even if it resembles one, but
|
| 476 |
+
all pragma unary operator expressions within it are then processed as
|
| 477 |
+
specified in [[cpp.pragma.op]] below.
|
| 478 |
|
| 479 |
+
### Scope of macro definitions <a id="cpp.scope">[[cpp.scope]]</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 480 |
|
| 481 |
+
A macro definition lasts (independent of block structure) until a
|
| 482 |
+
corresponding `#undef` directive is encountered or (if none is
|
| 483 |
+
encountered) until the end of the translation unit. Macro definitions
|
| 484 |
+
have no significance after translation phase 4.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 485 |
|
| 486 |
+
A preprocessing directive of the form
|
| 487 |
|
| 488 |
+
``` bnf
|
| 489 |
+
'# undef' identifier new-line
|
|
|
|
|
|
|
|
|
|
| 490 |
```
|
| 491 |
|
| 492 |
+
causes the specified identifier no longer to be defined as a macro name.
|
| 493 |
+
It is ignored if the specified identifier is not currently defined as a
|
| 494 |
+
macro name.
|
| 495 |
|