From Jason Turner

[cpp.scope]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpvh19sthg/{from.md → to.md} +0 -187
tmp/tmpvh19sthg/{from.md → to.md} RENAMED
@@ -13,192 +13,5 @@ A preprocessing directive of the form
13
 
14
  causes the specified identifier no longer to be defined as a macro name.
15
  It is ignored if the specified identifier is not currently defined as a
16
  macro name.
17
 
18
- [*Example 1*:
19
-
20
- The simplest use of this facility is to define a “manifest constant”, as
21
- in
22
-
23
- ``` cpp
24
- #define TABSIZE 100
25
- int table[TABSIZE];
26
- ```
27
-
28
- — *end example*]
29
-
30
- [*Example 2*:
31
-
32
- The following defines a function-like macro whose value is the maximum
33
- of its arguments. It has the advantages of working for any compatible
34
- types of the arguments and of generating in-line code without the
35
- overhead of function calling. It has the disadvantages of evaluating one
36
- or the other of its arguments a second time (including side effects) and
37
- generating more code than a function if invoked several times. It also
38
- cannot have its address taken, as it has none.
39
-
40
- ``` cpp
41
- #define max(a, b) ((a) > (b) ? (a) : (b))
42
- ```
43
-
44
- The parentheses ensure that the arguments and the resulting expression
45
- are bound properly.
46
-
47
- — *end example*]
48
-
49
- [*Example 3*:
50
-
51
- To illustrate the rules for redefinition and reexamination, the sequence
52
-
53
- ``` cpp
54
- #define x 3
55
- #define f(a) f(x * (a))
56
- #undef x
57
- #define x 2
58
- #define g f
59
- #define z z[0]
60
- #define h g(~
61
- #define m(a) a(w)
62
- #define w 0,1
63
- #define t(a) a
64
- #define p() int
65
- #define q(x) x
66
- #define r(x,y) x ## y
67
- #define str(x) # x
68
-
69
- f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);
70
- g(x+(3,4)-w) | h 5) & m
71
- (f)^m(m);
72
- p() i[q()] = { q(1), r(2,3), r(4,), r(,5), r(,) };
73
- char c[2][6] = { str(hello), str() };
74
- ```
75
-
76
- results in
77
-
78
- ``` cpp
79
- f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1);
80
- f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1);
81
- int i[] = { 1, 23, 4, 5, };
82
- char c[2][6] = { "hello", "" };
83
- ```
84
-
85
- — *end example*]
86
-
87
- [*Example 4*:
88
-
89
- To illustrate the rules for creating character string literals and
90
- concatenating tokens, the sequence
91
-
92
- ``` cpp
93
- #define str(s) # s
94
- #define xstr(s) str(s)
95
- #define debug(s, t) printf("x" # s "= %d, x" # t "= %s", \
96
- x ## s, x ## t)
97
- #define INCFILE(n) vers ## n
98
- #define glue(a, b) a ## b
99
- #define xglue(a, b) glue(a, b)
100
- #define HIGHLOW "hello"
101
- #define LOW LOW ", world"
102
-
103
- debug(1, 2);
104
- fputs(str(strncmp("abc\0d", "abc", '\4') // this goes away
105
- == 0) str(: \@n), s);
106
- #include xstr(INCFILE(2).h)
107
- glue(HIGH, LOW);
108
- xglue(HIGH, LOW)
109
- ```
110
-
111
- results in
112
-
113
- ``` cpp
114
- printf("x" "1" "= %d, x" "2" "= %s", x1, x2);
115
- fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0" ": \@n", s);
116
- #include "vers2.h" (after macro replacement, before file access)
117
- "hello";
118
- "hello" ", world"
119
- ```
120
-
121
- or, after concatenation of the character string literals,
122
-
123
- ``` cpp
124
- printf("x1= %d, x2= %s", x1, x2);
125
- fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0: \@n", s);
126
- #include "vers2.h" (after macro replacement, before file access)
127
- "hello";
128
- "hello, world"
129
- ```
130
-
131
- Space around the `#` and `##` tokens in the macro definition is
132
- optional.
133
-
134
- — *end example*]
135
-
136
- [*Example 5*:
137
-
138
- To illustrate the rules for placemarker preprocessing tokens, the
139
- sequence
140
-
141
- ``` cpp
142
- #define t(x,y,z) x ## y ## z
143
- int j[] = { t(1,2,3), t(,4,5), t(6,,7), t(8,9,),
144
- t(10,,), t(,11,), t(,,12), t(,,) };
145
- ```
146
-
147
- results in
148
-
149
- ``` cpp
150
- int j[] = { 123, 45, 67, 89,
151
- 10, 11, 12, };
152
- ```
153
-
154
- — *end example*]
155
-
156
- [*Example 6*:
157
-
158
- To demonstrate the redefinition rules, the following sequence is valid.
159
-
160
- ``` cpp
161
- #define OBJ_LIKE (1-1)
162
- #define OBJ_LIKE /* white space */ (1-1) /* other */
163
- #define FUNC_LIKE(a) ( a )
164
- #define FUNC_LIKE( a )( /* note the white space */ \
165
- a /* other stuff on this line
166
- */ )
167
- ```
168
-
169
- But the following redefinitions are invalid:
170
-
171
- ``` cpp
172
- #define OBJ_LIKE (0) // different token sequence
173
- #define OBJ_LIKE (1 - 1) // different white space
174
- #define FUNC_LIKE(b) ( a ) // different parameter usage
175
- #define FUNC_LIKE(b) ( b ) // different parameter spelling
176
- ```
177
-
178
- — *end example*]
179
-
180
- [*Example 7*:
181
-
182
- Finally, to show the variable argument list macro facilities:
183
-
184
- ``` cpp
185
- #define debug(...) fprintf(stderr, __VA_ARGS__)
186
- #define showlist(...) puts(#__VA_ARGS__)
187
- #define report(test, ...) ((test) ? puts(#test) : printf(__VA_ARGS__))
188
- debug("Flag");
189
- debug("X = %d\n", x);
190
- showlist(The first, second, and third items.);
191
- report(x>y, "x is %d but y is %d", x, y);
192
- ```
193
-
194
- results in
195
-
196
- ``` cpp
197
- fprintf(stderr, "Flag");
198
- fprintf(stderr, "X = %d\n", x);
199
- puts("The first, second, and third items.");
200
- ((x>y) ? puts("x>y") : printf("x is %d but y is %d", x, y));
201
- ```
202
-
203
- — *end example*]
204
-
 
13
 
14
  causes the specified identifier no longer to be defined as a macro name.
15
  It is ignored if the specified identifier is not currently defined as a
16
  macro name.
17