tmp/tmpmoj9oz6p/{from.md → to.md}
RENAMED
|
@@ -1,15 +1,122 @@
|
|
| 1 |
### Argument substitution <a id="cpp.subst">[[cpp.subst]]</a>
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
After the arguments for the invocation of a function-like macro have
|
| 4 |
-
been identified, argument substitution takes place.
|
| 5 |
-
replacement list
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
An identifier `__VA_ARGS__` that occurs in the replacement list shall be
|
| 13 |
treated as if it were a parameter, and the variable arguments shall form
|
| 14 |
the preprocessing tokens used to replace it.
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
### Argument substitution <a id="cpp.subst">[[cpp.subst]]</a>
|
| 2 |
|
| 3 |
+
``` bnf
|
| 4 |
+
va-opt-replacement:
|
| 5 |
+
'__VA_OPT__ (' pp-tokensₒₚₜ ')'
|
| 6 |
+
```
|
| 7 |
+
|
| 8 |
After the arguments for the invocation of a function-like macro have
|
| 9 |
+
been identified, argument substitution takes place. For each parameter
|
| 10 |
+
in the replacement list that is neither preceded by a `#` or `##`
|
| 11 |
+
preprocessing token nor followed by a `##` preprocessing token, the
|
| 12 |
+
preprocessing tokens naming the parameter are replaced by a token
|
| 13 |
+
sequence determined as follows:
|
| 14 |
+
|
| 15 |
+
- If the parameter is of the form *va-opt-replacement*, the replacement
|
| 16 |
+
preprocessing tokens are the preprocessing token sequence for the
|
| 17 |
+
corresponding argument, as specified below.
|
| 18 |
+
- Otherwise, the replacement preprocessing tokens are the preprocessing
|
| 19 |
+
tokens of corresponding argument after all macros contained therein
|
| 20 |
+
have been expanded. The argument’s preprocessing tokens are completely
|
| 21 |
+
macro replaced before being substituted as if they formed the rest of
|
| 22 |
+
the preprocessing file with no other preprocessing tokens being
|
| 23 |
+
available.
|
| 24 |
+
|
| 25 |
+
[*Example 1*:
|
| 26 |
+
|
| 27 |
+
``` cpp
|
| 28 |
+
#define LPAREN() (
|
| 29 |
+
#define G(Q) 42
|
| 30 |
+
#define F(R, X, ...) __VA_OPT__(G R X) )
|
| 31 |
+
int x = F(LPAREN(), 0, <:-); // replaced by int x = 42;
|
| 32 |
+
```
|
| 33 |
+
|
| 34 |
+
— *end example*]
|
| 35 |
|
| 36 |
An identifier `__VA_ARGS__` that occurs in the replacement list shall be
|
| 37 |
treated as if it were a parameter, and the variable arguments shall form
|
| 38 |
the preprocessing tokens used to replace it.
|
| 39 |
|
| 40 |
+
[*Example 2*:
|
| 41 |
+
|
| 42 |
+
``` cpp
|
| 43 |
+
#define debug(...) fprintf(stderr, __VA_ARGS__)
|
| 44 |
+
#define showlist(...) puts(#__VA_ARGS__)
|
| 45 |
+
#define report(test, ...) ((test) ? puts(#test) : printf(__VA_ARGS__))
|
| 46 |
+
debug("Flag");
|
| 47 |
+
debug("X = %d\n", x);
|
| 48 |
+
showlist(The first, second, and third items.);
|
| 49 |
+
report(x>y, "x is %d but y is %d", x, y);
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
results in
|
| 53 |
+
|
| 54 |
+
``` cpp
|
| 55 |
+
fprintf(stderr, "Flag");
|
| 56 |
+
fprintf(stderr, "X = %d\n", x);
|
| 57 |
+
puts("The first, second, and third items.");
|
| 58 |
+
((x>y) ? puts("x>y") : printf("x is %d but y is %d", x, y));
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
— *end example*]
|
| 62 |
+
|
| 63 |
+
The identifier `__VA_OPT__` shall always occur as part of the
|
| 64 |
+
preprocessing token sequence *va-opt-replacement*; its closing `)` is
|
| 65 |
+
determined by skipping intervening pairs of matching left and right
|
| 66 |
+
parentheses in its *pp-tokens*. The *pp-tokens* of a
|
| 67 |
+
*va-opt-replacement* shall not contain `__VA_OPT__`. If the *pp-tokens*
|
| 68 |
+
would be ill-formed as the replacement list of the current function-like
|
| 69 |
+
macro, the program is ill-formed. A *va-opt-replacement* is treated as
|
| 70 |
+
if it were a parameter, and the preprocessing token sequence for the
|
| 71 |
+
corresponding argument is defined as follows. If the substitution of
|
| 72 |
+
`__VA_ARGS__` as neither an operand of `#` nor `##` consists of no
|
| 73 |
+
preprocessing tokens, the argument consists of a single placemarker
|
| 74 |
+
preprocessing token ([[cpp.concat]], [[cpp.rescan]]). Otherwise, the
|
| 75 |
+
argument consists of the results of the expansion of the contained
|
| 76 |
+
*pp-tokens* as the replacement list of the current function-like macro
|
| 77 |
+
before removal of placemarker tokens, rescanning, and further
|
| 78 |
+
replacement.
|
| 79 |
+
|
| 80 |
+
[*Note 1*: The placemarker tokens are removed before stringization
|
| 81 |
+
[[cpp.stringize]], and can be removed by rescanning and further
|
| 82 |
+
replacement [[cpp.rescan]]. — *end note*]
|
| 83 |
+
|
| 84 |
+
[*Example 3*:
|
| 85 |
+
|
| 86 |
+
``` cpp
|
| 87 |
+
#define F(...) f(0 __VA_OPT__(,) __VA_ARGS__)
|
| 88 |
+
#define G(X, ...) f(0, X __VA_OPT__(,) __VA_ARGS__)
|
| 89 |
+
#define SDEF(sname, ...) S sname __VA_OPT__(= { __VA_ARGS__ })
|
| 90 |
+
#define EMP
|
| 91 |
+
|
| 92 |
+
F(a, b, c) // replaced by f(0, a, b, c)
|
| 93 |
+
F() // replaced by f(0)
|
| 94 |
+
F(EMP) // replaced by f(0)
|
| 95 |
+
|
| 96 |
+
G(a, b, c) // replaced by f(0, a, b, c)
|
| 97 |
+
G(a, ) // replaced by f(0, a)
|
| 98 |
+
G(a) // replaced by f(0, a)
|
| 99 |
+
|
| 100 |
+
SDEF(foo); // replaced by S foo;
|
| 101 |
+
SDEF(bar, 1, 2); // replaced by S bar = { 1, 2 }
|
| 102 |
+
|
| 103 |
+
#define H1(X, ...) X __VA_OPT__(##) __VA_ARGS__ // error: ## may not appear at
|
| 104 |
+
// the beginning of a replacement list[cpp.concat]
|
| 105 |
+
|
| 106 |
+
#define H2(X, Y, ...) __VA_OPT__(X ## Y,) __VA_ARGS__
|
| 107 |
+
H2(a, b, c, d) // replaced by ab, c, d
|
| 108 |
+
|
| 109 |
+
#define H3(X, ...) #__VA_OPT__(X##X X##X)
|
| 110 |
+
H3(, 0) // replaced by ""
|
| 111 |
+
|
| 112 |
+
#define H4(X, ...) __VA_OPT__(a X ## X) ## b
|
| 113 |
+
H4(, 1) // replaced by a b
|
| 114 |
+
|
| 115 |
+
#define H5A(...) __VA_OPT__()/**/__VA_OPT__()
|
| 116 |
+
#define H5B(X) a ## X ## b
|
| 117 |
+
#define H5C(X) H5B(X)
|
| 118 |
+
H5C(H5A()) // replaced by ab
|
| 119 |
+
```
|
| 120 |
+
|
| 121 |
+
— *end example*]
|
| 122 |
+
|