tmp/tmphyo7l98a/{from.md → to.md}
RENAMED
|
@@ -2,33 +2,34 @@
|
|
| 2 |
|
| 3 |
Two replacement lists are identical if and only if the preprocessing
|
| 4 |
tokens in both have the same number, ordering, spelling, and white-space
|
| 5 |
separation, where all white-space separations are considered identical.
|
| 6 |
|
| 7 |
-
An identifier currently defined as an
|
| 8 |
-
redefined by another `#define` preprocessing directive provided that the
|
| 9 |
-
second definition is an object-like macro definition and the two
|
| 10 |
-
replacement lists are identical, otherwise the program is ill-formed.
|
| 11 |
-
Likewise, an identifier currently defined as a *function-like* macro may
|
| 12 |
be redefined by another `#define` preprocessing directive provided that
|
| 13 |
-
the second definition is
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 more arguments in the invocation than there are
|
| 25 |
parameters in the macro definition (excluding the `...`). There shall
|
| 26 |
exist a `)` preprocessing token that terminates the invocation.
|
| 27 |
|
| 28 |
-
The identifier `__VA_ARGS__` shall occur only in the replacement-list
|
| 29 |
-
a function-like macro that uses the ellipsis notation in the
|
|
|
|
| 30 |
|
| 31 |
A parameter identifier in a function-like macro shall be uniquely
|
| 32 |
declared within its scope.
|
| 33 |
|
| 34 |
The identifier immediately following the `define` is called the *macro
|
|
@@ -46,12 +47,12 @@ A preprocessing directive of the form
|
|
| 46 |
``` bnf
|
| 47 |
'# define' identifier replacement-list new-line
|
| 48 |
```
|
| 49 |
|
| 50 |
defines an *object-like macro* that causes each subsequent instance of
|
| 51 |
-
the macro name[^
|
| 52 |
-
preprocessing tokens that constitute the remainder of the directive.[^
|
| 53 |
The replacement list is then rescanned for more macro names as specified
|
| 54 |
below.
|
| 55 |
|
| 56 |
A preprocessing directive of the form
|
| 57 |
|
|
@@ -79,11 +80,11 @@ The sequence of preprocessing tokens bounded by the outside-most
|
|
| 79 |
matching parentheses forms the list of arguments for the function-like
|
| 80 |
macro. The individual arguments within the list are separated by comma
|
| 81 |
preprocessing tokens, but comma preprocessing tokens between matching
|
| 82 |
inner parentheses do not separate arguments. If there are sequences of
|
| 83 |
preprocessing tokens within the list of arguments that would otherwise
|
| 84 |
-
act as preprocessing directives,[^
|
| 85 |
|
| 86 |
If there is a `...` immediately preceding the `)` in the function-like
|
| 87 |
macro definition, then the trailing arguments, including any separating
|
| 88 |
comma preprocessing tokens, are merged to form a single item: the
|
| 89 |
*variable arguments*. The number of arguments so combined is such that,
|
|
@@ -138,11 +139,11 @@ end of a replacement list for either form of macro definition.
|
|
| 138 |
If, in the replacement list of a function-like macro, a parameter is
|
| 139 |
immediately preceded or followed by a `##` preprocessing token, the
|
| 140 |
parameter is replaced by the corresponding argument’s preprocessing
|
| 141 |
token sequence; however, if an argument consists of no preprocessing
|
| 142 |
tokens, the parameter is replaced by a placemarker preprocessing token
|
| 143 |
-
instead.[^
|
| 144 |
|
| 145 |
For both object-like and function-like macro invocations, before the
|
| 146 |
replacement list is reexamined for more macro names to replace, each
|
| 147 |
instance of a `##` preprocessing token in the replacement list (not from
|
| 148 |
an argument) is deleted and the preceding preprocessing token is
|
|
@@ -153,19 +154,20 @@ concatenation of a placemarker with a non-placemarker preprocessing
|
|
| 153 |
token results in the non-placemarker preprocessing token. If the result
|
| 154 |
is not a valid preprocessing token, the behavior is undefined. The
|
| 155 |
resulting token is available for further macro replacement. The order of
|
| 156 |
evaluation of `##` operators is unspecified.
|
| 157 |
|
|
|
|
|
|
|
| 158 |
In the following fragment:
|
| 159 |
|
| 160 |
``` cpp
|
| 161 |
#define hash_hash # ## #
|
| 162 |
#define mkstr(a) # a
|
| 163 |
#define in_between(a) mkstr(a)
|
| 164 |
#define join(c, d) in_between(c hash_hash d)
|
| 165 |
-
char p[] = join(x, y); // equivalent to
|
| 166 |
-
// char p[] = "x ## y";
|
| 167 |
```
|
| 168 |
|
| 169 |
The expansion produces, at various stages:
|
| 170 |
|
| 171 |
``` cpp
|
|
@@ -178,10 +180,12 @@ mkstr(x ## y)
|
|
| 178 |
|
| 179 |
In other words, expanding `hash_hash` produces a new token, consisting
|
| 180 |
of two adjacent sharp signs, but this new token is not the `##`
|
| 181 |
operator.
|
| 182 |
|
|
|
|
|
|
|
| 183 |
### Rescanning and further replacement <a id="cpp.rescan">[[cpp.rescan]]</a>
|
| 184 |
|
| 185 |
After all parameters in the replacement list have been substituted and
|
| 186 |
`#` and `##` processing has taken place, all placemarker preprocessing
|
| 187 |
tokens are removed. Then the resulting preprocessing token sequence is
|
|
@@ -217,18 +221,24 @@ A preprocessing directive of the form
|
|
| 217 |
|
| 218 |
causes the specified identifier no longer to be defined as a macro name.
|
| 219 |
It is ignored if the specified identifier is not currently defined as a
|
| 220 |
macro name.
|
| 221 |
|
| 222 |
-
|
|
|
|
|
|
|
| 223 |
in
|
| 224 |
|
| 225 |
``` cpp
|
| 226 |
#define TABSIZE 100
|
| 227 |
int table[TABSIZE];
|
| 228 |
```
|
| 229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
The following defines a function-like macro whose value is the maximum
|
| 231 |
of its arguments. It has the advantages of working for any compatible
|
| 232 |
types of the arguments and of generating in-line code without the
|
| 233 |
overhead of function calling. It has the disadvantages of evaluating one
|
| 234 |
or the other of its arguments a second time (including side effects) and
|
|
@@ -240,10 +250,14 @@ cannot have its address taken, as it has none.
|
|
| 240 |
```
|
| 241 |
|
| 242 |
The parentheses ensure that the arguments and the resulting expression
|
| 243 |
are bound properly.
|
| 244 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
To illustrate the rules for redefinition and reexamination, the sequence
|
| 246 |
|
| 247 |
``` cpp
|
| 248 |
#define x 3
|
| 249 |
#define f(a) f(x * (a))
|
|
@@ -274,17 +288,22 @@ f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1);
|
|
| 274 |
f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1);
|
| 275 |
int i[] = { 1, 23, 4, 5, };
|
| 276 |
char c[2][6] = { "hello", "" };
|
| 277 |
```
|
| 278 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
To illustrate the rules for creating character string literals and
|
| 280 |
concatenating tokens, the sequence
|
| 281 |
|
| 282 |
``` cpp
|
| 283 |
#define str(s) # s
|
| 284 |
#define xstr(s) str(s)
|
| 285 |
-
#define debug(s, t) printf("x" # s "= %d, x" # t "= %s", \
|
|
|
|
| 286 |
#define INCFILE(n) vers ## n
|
| 287 |
#define glue(a, b) a ## b
|
| 288 |
#define xglue(a, b) glue(a, b)
|
| 289 |
#define HIGHLOW "hello"
|
| 290 |
#define LOW LOW ", world"
|
|
@@ -318,10 +337,14 @@ fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0: \@n", s);
|
|
| 318 |
```
|
| 319 |
|
| 320 |
Space around the `#` and `##` tokens in the macro definition is
|
| 321 |
optional.
|
| 322 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
To illustrate the rules for placemarker preprocessing tokens, the
|
| 324 |
sequence
|
| 325 |
|
| 326 |
``` cpp
|
| 327 |
#define t(x,y,z) x ## y ## z
|
|
@@ -334,17 +357,22 @@ results in
|
|
| 334 |
``` cpp
|
| 335 |
int j[] = { 123, 45, 67, 89,
|
| 336 |
10, 11, 12, };
|
| 337 |
```
|
| 338 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
To demonstrate the redefinition rules, the following sequence is valid.
|
| 340 |
|
| 341 |
``` cpp
|
| 342 |
#define OBJ_LIKE (1-1)
|
| 343 |
#define OBJ_LIKE /* white space */ (1-1) /* other */
|
| 344 |
#define FUNC_LIKE(a) ( a )
|
| 345 |
-
#define FUNC_LIKE( a )( /* note the white space */ \
|
|
|
|
| 346 |
*/ )
|
| 347 |
```
|
| 348 |
|
| 349 |
But the following redefinitions are invalid:
|
| 350 |
|
|
@@ -353,10 +381,14 @@ But the following redefinitions are invalid:
|
|
| 353 |
#define OBJ_LIKE (1 - 1) // different white space
|
| 354 |
#define FUNC_LIKE(b) ( a ) // different parameter usage
|
| 355 |
#define FUNC_LIKE(b) ( b ) // different parameter spelling
|
| 356 |
```
|
| 357 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
Finally, to show the variable argument list macro facilities:
|
| 359 |
|
| 360 |
``` cpp
|
| 361 |
#define debug(...) fprintf(stderr, __VA_ARGS__)
|
| 362 |
#define showlist(...) puts(#__VA_ARGS__)
|
|
@@ -374,5 +406,7 @@ fprintf(stderr, "Flag");
|
|
| 374 |
fprintf(stderr, "X = %d\n", x);
|
| 375 |
puts("The first, second, and third items.");
|
| 376 |
((x>y) ? puts("x>y") : printf("x is %d but y is %d", x, y));
|
| 377 |
```
|
| 378 |
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
Two replacement lists are identical if and only if the preprocessing
|
| 4 |
tokens in both have the same number, ordering, spelling, and white-space
|
| 5 |
separation, where all white-space separations are considered identical.
|
| 6 |
|
| 7 |
+
An identifier currently defined as an object-like macro (see below) may
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
be redefined by another `#define` preprocessing directive provided that
|
| 9 |
+
the second definition is an object-like macro definition and the two
|
| 10 |
+
replacement lists are identical, otherwise the program is ill-formed.
|
| 11 |
+
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 more arguments in the invocation than there are
|
| 25 |
parameters in the macro definition (excluding the `...`). There shall
|
| 26 |
exist a `)` preprocessing token that terminates the invocation.
|
| 27 |
|
| 28 |
+
The identifier `__VA_ARGS__` shall occur only in the *replacement-list*
|
| 29 |
+
of a function-like macro that uses the ellipsis notation in the
|
| 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
|
|
|
|
| 47 |
``` bnf
|
| 48 |
'# define' identifier replacement-list new-line
|
| 49 |
```
|
| 50 |
|
| 51 |
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 |
|
|
|
|
| 80 |
matching parentheses forms the list of arguments for the function-like
|
| 81 |
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 separating
|
| 89 |
comma preprocessing tokens, are merged to form a single item: the
|
| 90 |
*variable arguments*. The number of arguments so combined is such that,
|
|
|
|
| 139 |
If, in the replacement list of a function-like macro, a parameter is
|
| 140 |
immediately preceded or followed by a `##` preprocessing token, the
|
| 141 |
parameter is replaced by the corresponding argument’s preprocessing
|
| 142 |
token sequence; however, if an argument consists of no preprocessing
|
| 143 |
tokens, the parameter is replaced by a placemarker preprocessing token
|
| 144 |
+
instead.[^8]
|
| 145 |
|
| 146 |
For both object-like and function-like macro invocations, before the
|
| 147 |
replacement list is reexamined for more macro names to replace, each
|
| 148 |
instance of a `##` preprocessing token in the replacement list (not from
|
| 149 |
an argument) is deleted and the preceding preprocessing token is
|
|
|
|
| 154 |
token results in the non-placemarker preprocessing token. If the result
|
| 155 |
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
|
| 166 |
#define in_between(a) mkstr(a)
|
| 167 |
#define join(c, d) in_between(c hash_hash d)
|
| 168 |
+
char p[] = join(x, y); // equivalent to char p[] = "x ## y";
|
|
|
|
| 169 |
```
|
| 170 |
|
| 171 |
The expansion produces, at various stages:
|
| 172 |
|
| 173 |
``` cpp
|
|
|
|
| 180 |
|
| 181 |
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
|
|
|
|
| 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 simplest use of this facility is to define a “manifest constant”, as
|
| 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
|
|
|
|
| 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))
|
|
|
|
| 288 |
f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1);
|
| 289 |
int i[] = { 1, 23, 4, 5, };
|
| 290 |
char c[2][6] = { "hello", "" };
|
| 291 |
```
|
| 292 |
|
| 293 |
+
— *end example*]
|
| 294 |
+
|
| 295 |
+
[*Example 4*:
|
| 296 |
+
|
| 297 |
To illustrate the rules for creating character string literals and
|
| 298 |
concatenating tokens, the sequence
|
| 299 |
|
| 300 |
``` cpp
|
| 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"
|
|
|
|
| 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
|
|
|
|
| 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 |
|
|
|
|
| 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__)
|
|
|
|
| 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 |
+
— *end example*]
|
| 412 |
+
|