From Jason Turner

[cpp]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpn375wp97/{from.md → to.md} +233 -95
tmp/tmpn375wp97/{from.md → to.md} RENAMED
@@ -22,14 +22,14 @@ group:
22
  group group-part
23
  ```
24
 
25
  ``` bnf
26
  group-part:
27
- if-section
28
  control-line
 
29
  text-line
30
- '#' non-directive
31
  ```
32
 
33
  ``` bnf
34
  if-section:
35
  if-group elif-groupsₒₚₜ else-groupₒₚₜ endif-line
@@ -45,11 +45,11 @@ elif-groups:
45
  text-line:
46
  pp-tokensₒₚₜ new-line
47
  ```
48
 
49
  ``` bnf
50
- non-directive:
51
  pp-tokens new-line
52
  ```
53
 
54
  ``` bnf
55
  lparen:
@@ -77,12 +77,14 @@ pp-tokens:
77
  new-line:
78
  the new-line character
79
  ```
80
 
81
  A text line shall not begin with a `#` preprocessing token. A
82
- non-directive shall not begin with any of the directive names appearing
83
- in the syntax.
 
 
84
 
85
  When in a group that is skipped ([[cpp.cond]]), the directive syntax is
86
  relaxed to allow any sequence of preprocessing tokens to occur between
87
  the directive name and the following new-line character.
88
 
@@ -99,43 +101,82 @@ capabilities are called *preprocessing*, because conceptually they occur
99
  before translation of the resulting translation unit.
100
 
101
  The preprocessing tokens within a preprocessing directive are not
102
  subject to macro expansion unless otherwise stated.
103
 
 
 
104
  In:
105
 
106
  ``` cpp
107
  #define EMPTY
108
  EMPTY # include <file.h>
109
  ```
110
 
111
  the sequence of preprocessing tokens on the second line is *not* a
112
- preprocessing directive, because it does not begin with a \# at the
113
  start of translation phase 4, even though it will do so after the macro
114
  `EMPTY` has been replaced.
115
 
 
 
116
  ## Conditional inclusion <a id="cpp.cond">[[cpp.cond]]</a>
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  The expression that controls conditional inclusion shall be an integral
119
  constant expression except that identifiers (including those lexically
120
  identical to keywords) are interpreted as described below[^2] and it may
121
- contain unary operator expressions of the form
 
122
 
123
- ``` bnf
124
- 'defined' identifier
125
- ```
 
 
126
 
127
- or
 
 
128
 
129
- ``` bnf
130
- 'defined (' identifier ')'
131
- ```
 
 
 
 
 
132
 
133
- which evaluate to `1` if the identifier is currently defined as a macro
134
- name (that is, if it is predefined or if it has been the subject of a
135
- `#define` preprocessing directive without an intervening `#undef`
136
- directive with the same subject identifier), `0` if it is not.
137
 
138
  Each preprocessing token that remains (in the list of preprocessing
139
  tokens that will become the controlling expression) after all macro
140
  replacements have occurred shall be in the lexical form of a token (
141
  [[lex.token]]).
@@ -148,29 +189,60 @@ Prior to evaluation, macro invocations in the list of preprocessing
148
  tokens that will become the controlling constant expression are replaced
149
  (except for those macro names modified by the `defined` unary operator),
150
  just as in normal text. If the token `defined` is generated as a result
151
  of this replacement process or use of the `defined` unary operator does
152
  not match one of the two specified forms prior to macro replacement, the
153
- behavior is undefined. After all replacements due to macro expansion and
154
- the `defined` unary operator have been performed, all remaining
155
- identifiers and keywords[^3], except for `true` and `false`, are
156
- replaced with the pp-number `0`, and then each preprocessing token is
157
- converted into a token. The resulting tokens comprise the controlling
158
- constant expression which is evaluated according to the rules of 
159
- [[expr.const]] using arithmetic that has at least the ranges specified
160
- in  [[support.limits]]. For the purposes of this token conversion and
161
- evaluation all signed and unsigned integer types act as if they have the
162
- same representation as, respectively, `intmax_t` or `uintmax_t` (
163
- [[cstdint]]).[^4] This includes interpreting character literals, which
164
- may involve converting escape sequences into execution character set
165
- members. Whether the numeric value for these character literals matches
166
- the value obtained when an identical character literal occurs in an
167
- expression (other than within a `#if` or `#elif` directive) is
168
- *implementation-defined*.[^5] Also, whether a single-character character
169
- literal may have a negative value is *implementation-defined*. Each
170
- subexpression with type `bool` is subjected to integral promotion before
171
- processing continues.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
 
173
  Preprocessing directives of the forms
174
 
175
  check whether the identifier is or is not currently defined as a macro
176
  name. Their conditions are equivalent to `#if` `defined` *identifier*
@@ -180,14 +252,36 @@ Each directive’s condition is checked in order. If it evaluates to false
180
  (zero), the group that it controls is skipped: directives are processed
181
  only through the name that determines the directive in order to keep
182
  track of the level of nested conditionals; the rest of the directives’
183
  preprocessing tokens are ignored, as are the other preprocessing tokens
184
  in the group. Only the first group whose control condition evaluates to
185
- true (nonzero) is processed. If none of the conditions evaluates to
186
- true, and there is a `#else` directive, the group controlled by the
187
- `#else` is processed; lacking a `#else` directive, all the groups until
188
- the `#endif` are skipped.[^6]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
 
190
  ## Source file inclusion <a id="cpp.include">[[cpp.include]]</a>
191
 
192
  A `#include` directive shall identify a header or source file that can
193
  be processed by the implementation.
@@ -232,11 +326,11 @@ A preprocessing directive of the form
232
  (that does not match one of the two previous forms) is permitted. The
233
  preprocessing tokens after `include` in the directive are processed just
234
  as in normal text (i.e., each identifier currently defined as a macro
235
  name is replaced by its replacement list of preprocessing tokens). If
236
  the directive resulting after all replacements does not match one of the
237
- two previous forms, the behavior is undefined.[^7] The method by which a
238
  sequence of preprocessing tokens between a `<` and a `>` preprocessing
239
  token pair or a pair of `"` characters is combined into a single header
240
  name preprocessing token is *implementation-defined*.
241
 
242
  The implementation shall provide unique mappings for sequences
@@ -247,10 +341,12 @@ alphabetical case.
247
 
248
  A `#include` preprocessing directive may appear in a source file that
249
  has been read because of a `#include` directive in another file, up to
250
  an *implementation-defined* nesting limit.
251
 
 
 
252
  Although an implementation may provide a mechanism for making arbitrary
253
  source files available to the `< >` search, in general programmers
254
  should use the `< >` form for headers provided with the implementation,
255
  and the `" "` form for sources outside the control of the
256
  implementation. For instance:
@@ -260,10 +356,14 @@ implementation. For instance:
260
  #include <unistd.h>
261
  #include "usefullib.h"
262
  #include "myprog.h"
263
  ```
264
 
 
 
 
 
265
  This illustrates macro-replaced `#include` directives:
266
 
267
  ``` cpp
268
  #if VERSION == 1
269
  #define INCFILE "vers1.h"
@@ -273,39 +373,42 @@ This illustrates macro-replaced `#include` directives:
273
  #define INCFILE "versN.h"
274
  #endif
275
  #include INCFILE
276
  ```
277
 
 
 
278
  ## Macro replacement <a id="cpp.replace">[[cpp.replace]]</a>
279
 
280
  Two replacement lists are identical if and only if the preprocessing
281
  tokens in both have the same number, ordering, spelling, and white-space
282
  separation, where all white-space separations are considered identical.
283
 
284
- An identifier currently defined as an *object-like* macro may be
285
- redefined by another `#define` preprocessing directive provided that the
286
- second definition is an object-like macro definition and the two
287
- replacement lists are identical, otherwise the program is ill-formed.
288
- Likewise, an identifier currently defined as a *function-like* macro may
289
  be redefined by another `#define` preprocessing directive provided that
290
- the second definition is a function-like macro definition that has the
291
- same number and spelling of parameters, and the two replacement lists
292
- are identical, otherwise the program is ill-formed.
 
 
 
 
293
 
294
  There shall be white-space between the identifier and the replacement
295
  list in the definition of an object-like macro.
296
 
297
- If the identifier-list in the macro definition does not end with an
298
  ellipsis, the number of arguments (including those arguments consisting
299
  of no preprocessing tokens) in an invocation of a function-like macro
300
  shall equal the number of parameters in the macro definition. Otherwise,
301
  there shall be more arguments in the invocation than there are
302
  parameters in the macro definition (excluding the `...`). There shall
303
  exist a `)` preprocessing token that terminates the invocation.
304
 
305
- The identifier `__VA_ARGS__` shall occur only in the replacement-list of
306
- a function-like macro that uses the ellipsis notation in the parameters.
 
307
 
308
  A parameter identifier in a function-like macro shall be uniquely
309
  declared within its scope.
310
 
311
  The identifier immediately following the `define` is called the *macro
@@ -323,12 +426,12 @@ A preprocessing directive of the form
323
  ``` bnf
324
  '# define' identifier replacement-list new-line
325
  ```
326
 
327
  defines an *object-like macro* that causes each subsequent instance of
328
- the macro name[^8] to be replaced by the replacement list of
329
- preprocessing tokens that constitute the remainder of the directive.[^9]
330
  The replacement list is then rescanned for more macro names as specified
331
  below.
332
 
333
  A preprocessing directive of the form
334
 
@@ -356,11 +459,11 @@ The sequence of preprocessing tokens bounded by the outside-most
356
  matching parentheses forms the list of arguments for the function-like
357
  macro. The individual arguments within the list are separated by comma
358
  preprocessing tokens, but comma preprocessing tokens between matching
359
  inner parentheses do not separate arguments. If there are sequences of
360
  preprocessing tokens within the list of arguments that would otherwise
361
- act as preprocessing directives,[^10] the behavior is undefined.
362
 
363
  If there is a `...` immediately preceding the `)` in the function-like
364
  macro definition, then the trailing arguments, including any separating
365
  comma preprocessing tokens, are merged to form a single item: the
366
  *variable arguments*. The number of arguments so combined is such that,
@@ -415,11 +518,11 @@ end of a replacement list for either form of macro definition.
415
  If, in the replacement list of a function-like macro, a parameter is
416
  immediately preceded or followed by a `##` preprocessing token, the
417
  parameter is replaced by the corresponding argument’s preprocessing
418
  token sequence; however, if an argument consists of no preprocessing
419
  tokens, the parameter is replaced by a placemarker preprocessing token
420
- instead.[^11]
421
 
422
  For both object-like and function-like macro invocations, before the
423
  replacement list is reexamined for more macro names to replace, each
424
  instance of a `##` preprocessing token in the replacement list (not from
425
  an argument) is deleted and the preceding preprocessing token is
@@ -430,19 +533,20 @@ concatenation of a placemarker with a non-placemarker preprocessing
430
  token results in the non-placemarker preprocessing token. If the result
431
  is not a valid preprocessing token, the behavior is undefined. The
432
  resulting token is available for further macro replacement. The order of
433
  evaluation of `##` operators is unspecified.
434
 
 
 
435
  In the following fragment:
436
 
437
  ``` cpp
438
  #define hash_hash # ## #
439
  #define mkstr(a) # a
440
  #define in_between(a) mkstr(a)
441
  #define join(c, d) in_between(c hash_hash d)
442
- char p[] = join(x, y); // equivalent to
443
- // char p[] = "x ## y";
444
  ```
445
 
446
  The expansion produces, at various stages:
447
 
448
  ``` cpp
@@ -455,10 +559,12 @@ mkstr(x ## y)
455
 
456
  In other words, expanding `hash_hash` produces a new token, consisting
457
  of two adjacent sharp signs, but this new token is not the `##`
458
  operator.
459
 
 
 
460
  ### Rescanning and further replacement <a id="cpp.rescan">[[cpp.rescan]]</a>
461
 
462
  After all parameters in the replacement list have been substituted and
463
  `#` and `##` processing has taken place, all placemarker preprocessing
464
  tokens are removed. Then the resulting preprocessing token sequence is
@@ -494,18 +600,24 @@ A preprocessing directive of the form
494
 
495
  causes the specified identifier no longer to be defined as a macro name.
496
  It is ignored if the specified identifier is not currently defined as a
497
  macro name.
498
 
499
- The simplest use of this facility is to define a “manifest constant,” as
 
 
500
  in
501
 
502
  ``` cpp
503
  #define TABSIZE 100
504
  int table[TABSIZE];
505
  ```
506
 
 
 
 
 
507
  The following defines a function-like macro whose value is the maximum
508
  of its arguments. It has the advantages of working for any compatible
509
  types of the arguments and of generating in-line code without the
510
  overhead of function calling. It has the disadvantages of evaluating one
511
  or the other of its arguments a second time (including side effects) and
@@ -517,10 +629,14 @@ cannot have its address taken, as it has none.
517
  ```
518
 
519
  The parentheses ensure that the arguments and the resulting expression
520
  are bound properly.
521
 
 
 
 
 
522
  To illustrate the rules for redefinition and reexamination, the sequence
523
 
524
  ``` cpp
525
  #define x 3
526
  #define f(a) f(x * (a))
@@ -551,17 +667,22 @@ f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1);
551
  f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1);
552
  int i[] = { 1, 23, 4, 5, };
553
  char c[2][6] = { "hello", "" };
554
  ```
555
 
 
 
 
 
556
  To illustrate the rules for creating character string literals and
557
  concatenating tokens, the sequence
558
 
559
  ``` cpp
560
  #define str(s) # s
561
  #define xstr(s) str(s)
562
- #define debug(s, t) printf("x" # s "= %d, x" # t "= %s", \ x ## s, x ## t)
 
563
  #define INCFILE(n) vers ## n
564
  #define glue(a, b) a ## b
565
  #define xglue(a, b) glue(a, b)
566
  #define HIGHLOW "hello"
567
  #define LOW LOW ", world"
@@ -595,10 +716,14 @@ fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0: \@n", s);
595
  ```
596
 
597
  Space around the `#` and `##` tokens in the macro definition is
598
  optional.
599
 
 
 
 
 
600
  To illustrate the rules for placemarker preprocessing tokens, the
601
  sequence
602
 
603
  ``` cpp
604
  #define t(x,y,z) x ## y ## z
@@ -611,17 +736,22 @@ results in
611
  ``` cpp
612
  int j[] = { 123, 45, 67, 89,
613
  10, 11, 12, };
614
  ```
615
 
 
 
 
 
616
  To demonstrate the redefinition rules, the following sequence is valid.
617
 
618
  ``` cpp
619
  #define OBJ_LIKE (1-1)
620
  #define OBJ_LIKE /* white space */ (1-1) /* other */
621
  #define FUNC_LIKE(a) ( a )
622
- #define FUNC_LIKE( a )( /* note the white space */ \ a /* other stuff on this line
 
623
  */ )
624
  ```
625
 
626
  But the following redefinitions are invalid:
627
 
@@ -630,10 +760,14 @@ But the following redefinitions are invalid:
630
  #define OBJ_LIKE (1 - 1) // different white space
631
  #define FUNC_LIKE(b) ( a ) // different parameter usage
632
  #define FUNC_LIKE(b) ( b ) // different parameter spelling
633
  ```
634
 
 
 
 
 
635
  Finally, to show the variable argument list macro facilities:
636
 
637
  ``` cpp
638
  #define debug(...) fprintf(stderr, __VA_ARGS__)
639
  #define showlist(...) puts(#__VA_ARGS__)
@@ -651,10 +785,12 @@ fprintf(stderr, "Flag");
651
  fprintf(stderr, "X = %d\n", x);
652
  puts("The first, second, and third items.");
653
  ((x>y) ? puts("x>y") : printf("x is %d but y is %d", x, y));
654
  ```
655
 
 
 
656
  ## Line control <a id="cpp.line">[[cpp.line]]</a>
657
 
658
  The string literal of a `#line` directive, if present, shall be a
659
  character string literal.
660
 
@@ -736,14 +872,13 @@ has no effect.
736
 
737
  ## Predefined macro names <a id="cpp.predefined">[[cpp.predefined]]</a>
738
 
739
  The following macro names shall be defined by the implementation:
740
 
741
- - **`\texttt{__cplusplus}`**
742
 
743
- The name ` __cplusplus` is defined to the value `202302L` when compiling
744
- a C++translation unit.[^12]
745
 
746
  - **`__DATE__`**
747
 
748
  The date of translation of the source file: a character string literal
749
  of the form `"Mmm dd yyyy"`, where the names of the months are the same
@@ -753,24 +888,32 @@ translation is not available, an *implementation-defined* valid date
753
  shall be supplied.
754
 
755
  - **`__FILE__`**
756
 
757
  The presumed name of the current source file (a character string
758
- literal).[^13]
759
 
760
  - **`__LINE__`**
761
 
762
  The presumed line number (within the current source file) of the current
763
- source line (an integer literal).
764
-
765
-  
766
 
767
  - **`__STDC_HOSTED__`**
768
 
769
  The integer literal `1` if the implementation is a hosted implementation
770
  or the integer literal `0` if it is not.
771
 
 
 
 
 
 
 
 
 
 
 
772
  - **`__TIME__`**
773
 
774
  The time of translation of the source file: a character string literal
775
  of the form `"hh:mm:ss"` as in the time generated by the `asctime`
776
  function. If the time of translation is not available, an
@@ -841,10 +984,12 @@ and replacing each escape sequence `\\` by a single backslash. The
841
  resulting sequence of characters is processed through translation phase
842
  3 to produce preprocessing tokens that are executed as if they were the
843
  *pp-tokens* in a pragma directive. The original four preprocessing
844
  tokens in the unary operator expression are removed.
845
 
 
 
846
  ``` cpp
847
  #pragma listing on "..\listing.dir"
848
  ```
849
 
850
  can also be expressed as:
@@ -861,10 +1006,12 @@ literally as shown, or results from macro replacement, as in:
861
  #define PRAGMA(x) _Pragma(#x)
862
 
863
  LISTING( ..\listing.dir )
864
  ```
865
 
 
 
866
  <!-- Link reference definitions -->
867
  [basic.stc.dynamic.safety]: basic.md#basic.stc.dynamic.safety
868
  [cpp]: #cpp
869
  [cpp.concat]: #cpp.concat
870
  [cpp.cond]: #cpp.cond
@@ -880,69 +1027,60 @@ LISTING( ..\listing.dir )
880
  [cpp.scope]: #cpp.scope
881
  [cpp.stringize]: #cpp.stringize
882
  [cpp.subst]: #cpp.subst
883
  [cstdint]: language.md#cstdint
884
  [expr.const]: expr.md#expr.const
 
885
  [intro.multithread]: intro.md#intro.multithread
886
  [lex.digraph]: lex.md#lex.digraph
887
  [lex.name]: lex.md#lex.name
888
  [lex.phases]: lex.md#lex.phases
889
  [lex.token]: lex.md#lex.token
890
  [support.limits]: language.md#support.limits
891
 
892
- [^1]: Thus, preprocessing directives are commonly called “lines.” These
893
  “lines” have no other syntactic significance, as all white space is
894
  equivalent except in certain situations during preprocessing (see
895
  the `#` character string literal creation operator in 
896
  [[cpp.stringize]], for example).
897
 
898
  [^2]: Because the controlling constant expression is evaluated during
899
  translation phase 4, all identifiers either are or are not macro
900
  names — there simply are no keywords, enumeration constants, etc.
901
 
902
- [^3]: An alternative token ([[lex.digraph]]) is not an identifier, even
903
- when its spelling consists entirely of letters and underscores.
904
- Therefore it is not subject to this replacement.
905
-
906
- [^4]: Thus on an implementation where `std::numeric_limits<int>::max()`
907
- is `0x7FFF` and `std::numeric_limits<unsigned int>::max()` is
908
- `0xFFFF`, the integer literal `0x8000` is signed and positive within
909
- a `#if` expression even though it is unsigned in translation phase
910
- 7 ([[lex.phases]]).
911
-
912
- [^5]: Thus, the constant expression in the following `#if` directive and
913
- `if` statement is not guaranteed to evaluate to the same value in
914
- these two contexts.
915
-
916
- [^6]: As indicated by the syntax, a preprocessing token shall not follow
917
  a `#else` or `#endif` directive before the terminating new-line
918
  character. However, comments may appear anywhere in a source file,
919
  including within a preprocessing directive.
920
 
921
- [^7]: Note that adjacent string literals are not concatenated into a
922
  single string literal (see the translation phases in 
923
  [[lex.phases]]); thus, an expansion that results in two string
924
  literals is an invalid directive.
925
 
926
- [^8]: Since, by macro-replacement time, all character literals and
927
  string literals are preprocessing tokens, not sequences possibly
928
  containing identifier-like subsequences (see [[lex.phases]],
929
  translation phases), they are never scanned for macro names or
930
  parameters.
931
 
932
- [^9]: An alternative token ([[lex.digraph]]) is not an identifier, even
933
  when its spelling consists entirely of letters and underscores.
934
  Therefore it is not possible to define a macro whose name is the
935
  same as that of an alternative token.
936
 
937
- [^10]: Despite the name, a non-directive is a preprocessing directive.
 
938
 
939
- [^11]: Placemarker preprocessing tokens do not appear in the syntax
940
  because they are temporary entities that exist only within
941
  translation phase 4.
942
 
943
- [^12]: It is intended that future versions of this standard will replace
944
- the value of this macro with a greater value. Non-conforming
945
- compilers should use a value with at most five decimal digits.
 
946
 
947
- [^13]: The presumed source file name and line number can be changed by
948
- the `#line` directive.
 
 
 
22
  group group-part
23
  ```
24
 
25
  ``` bnf
26
  group-part:
 
27
  control-line
28
+ if-section
29
  text-line
30
+ '#' conditionally-supported-directive
31
  ```
32
 
33
  ``` bnf
34
  if-section:
35
  if-group elif-groupsₒₚₜ else-groupₒₚₜ endif-line
 
45
  text-line:
46
  pp-tokensₒₚₜ new-line
47
  ```
48
 
49
  ``` bnf
50
+ conditionally-supported-directive:
51
  pp-tokens new-line
52
  ```
53
 
54
  ``` bnf
55
  lparen:
 
77
  new-line:
78
  the new-line character
79
  ```
80
 
81
  A text line shall not begin with a `#` preprocessing token. A
82
+ *conditionally-supported-directive* shall not begin with any of the
83
+ directive names appearing in the syntax. A
84
+ *conditionally-supported-directive* is conditionally-supported with
85
+ *implementation-defined* semantics.
86
 
87
  When in a group that is skipped ([[cpp.cond]]), the directive syntax is
88
  relaxed to allow any sequence of preprocessing tokens to occur between
89
  the directive name and the following new-line character.
90
 
 
101
  before translation of the resulting translation unit.
102
 
103
  The preprocessing tokens within a preprocessing directive are not
104
  subject to macro expansion unless otherwise stated.
105
 
106
+ [*Example 1*:
107
+
108
  In:
109
 
110
  ``` cpp
111
  #define EMPTY
112
  EMPTY # include <file.h>
113
  ```
114
 
115
  the sequence of preprocessing tokens on the second line is *not* a
116
+ preprocessing directive, because it does not begin with a `#` at the
117
  start of translation phase 4, even though it will do so after the macro
118
  `EMPTY` has been replaced.
119
 
120
+ — *end example*]
121
+
122
  ## Conditional inclusion <a id="cpp.cond">[[cpp.cond]]</a>
123
 
124
+ ``` bnf
125
+ defined-macro-expression:
126
+ 'defined' identifier
127
+ 'defined (' identifier ')'
128
+ ```
129
+
130
+ ``` bnf
131
+ h-preprocessing-token:
132
+ any *preprocessing-token* other than '>'
133
+ ```
134
+
135
+ ``` bnf
136
+ h-pp-tokens:
137
+ h-preprocessing-token
138
+ h-pp-tokens h-preprocessing-token
139
+ ```
140
+
141
+ ``` bnf
142
+ has-include-expression:
143
+ '__has_include ( <' h-char-sequence '> )'
144
+ '__has_include ( "' q-char-sequence '" )'
145
+ '__has_include (' string-literal ')'
146
+ '__has_include ( <' h-pp-tokens '> )'
147
+ ```
148
+
149
  The expression that controls conditional inclusion shall be an integral
150
  constant expression except that identifiers (including those lexically
151
  identical to keywords) are interpreted as described below[^2] and it may
152
+ contain zero or more *defined-macro-expression*s and/or
153
+ *has-include-expression*s as unary operator expressions.
154
 
155
+ A *defined-macro-expression* evaluates to `1` if the identifier is
156
+ currently defined as a macro name (that is, if it is predefined or if it
157
+ has been the subject of a `#define` preprocessing directive without an
158
+ intervening `#undef` directive with the same subject identifier), `0` if
159
+ it is not.
160
 
161
+ The third and fourth forms of *has-include-expression* are considered
162
+ only if neither of the first or second forms matches, in which case the
163
+ preprocessing tokens are processed just as in normal text.
164
 
165
+ The header or source file identified by the parenthesized preprocessing
166
+ token sequence in each contained *has-include-expression* is searched
167
+ for as if that preprocessing token sequence were the *pp-tokens* in a
168
+ `#include` directive, except that no further macro expansion is
169
+ performed. If such a directive would not satisfy the syntactic
170
+ requirements of a `#include` directive, the program is ill-formed. The
171
+ *has-include-expression* evaluates to `1` if the search for the source
172
+ file succeeds, and to `0` if the search fails.
173
 
174
+ The `#ifdef` and `#ifndef` directives, and the `defined` conditional
175
+ inclusion operator, shall treat `__has_include` as if it were the name
176
+ of a defined macro. The identifier `__has_include` shall not appear in
177
+ any context not mentioned in this section.
178
 
179
  Each preprocessing token that remains (in the list of preprocessing
180
  tokens that will become the controlling expression) after all macro
181
  replacements have occurred shall be in the lexical form of a token (
182
  [[lex.token]]).
 
189
  tokens that will become the controlling constant expression are replaced
190
  (except for those macro names modified by the `defined` unary operator),
191
  just as in normal text. If the token `defined` is generated as a result
192
  of this replacement process or use of the `defined` unary operator does
193
  not match one of the two specified forms prior to macro replacement, the
194
+ behavior is undefined.
195
+
196
+ After all replacements due to macro expansion and evaluations of
197
+ *defined-macro-expression*s and *has-include-expression*s have been
198
+ performed, all remaining identifiers and keywords, except for `true` and
199
+ `false`, are replaced with the *pp-number* `0`, and then each
200
+ preprocessing token is converted into a token.
201
+
202
+ [*Note 1*: An alternative token ([[lex.digraph]]) is not an
203
+ identifier, even when its spelling consists entirely of letters and
204
+ underscores. Therefore it is not subject to this
205
+ replacement. *end note*]
206
+
207
+ The resulting tokens comprise the controlling constant expression which
208
+ is evaluated according to the rules of  [[expr.const]] using arithmetic
209
+ that has at least the ranges specified in  [[support.limits]]. For the
210
+ purposes of this token conversion and evaluation all signed and unsigned
211
+ integer types act as if they have the same representation as,
212
+ respectively, `intmax_t` or `uintmax_t` ([[cstdint]]).
213
+
214
+ [*Note 2*: Thus on an implementation where
215
+ `std::numeric_limits<int>::max()` is `0x7FFF` and
216
+ `std::numeric_limits<unsigned int>::max()` is `0xFFFF`, the integer
217
+ literal `0x8000` is signed and positive within a `#if` expression even
218
+ though it is unsigned in translation phase 7 (
219
+ [[lex.phases]]). — *end note*]
220
+
221
+ This includes interpreting character literals, which may involve
222
+ converting escape sequences into execution character set members.
223
+ Whether the numeric value for these character literals matches the value
224
+ obtained when an identical character literal occurs in an expression
225
+ (other than within a `#if` or `#elif` directive) is
226
+ *implementation-defined*.
227
+
228
+ [*Note 3*:
229
+
230
+ Thus, the constant expression in the following `#if` directive and `if`
231
+ statement is not guaranteed to evaluate to the same value in these two
232
+ contexts:
233
+
234
+ ``` cpp
235
+ #if 'z' - 'a' == 25
236
+ if ('z' - 'a' == 25)
237
+ ```
238
+
239
+ — *end note*]
240
+
241
+ Also, whether a single-character character literal may have a negative
242
+ value is *implementation-defined*. Each subexpression with type `bool`
243
+ is subjected to integral promotion before processing continues.
244
 
245
  Preprocessing directives of the forms
246
 
247
  check whether the identifier is or is not currently defined as a macro
248
  name. Their conditions are equivalent to `#if` `defined` *identifier*
 
252
  (zero), the group that it controls is skipped: directives are processed
253
  only through the name that determines the directive in order to keep
254
  track of the level of nested conditionals; the rest of the directives’
255
  preprocessing tokens are ignored, as are the other preprocessing tokens
256
  in the group. Only the first group whose control condition evaluates to
257
+ true (nonzero) is processed; any following groups are skipped and their
258
+ controlling directives are processed as if they were in a group that is
259
+ skipped. If none of the conditions evaluates to true, and there is a
260
+ `#else` directive, the group controlled by the `#else` is processed;
261
+ lacking a `#else` directive, all the groups until the `#endif` are
262
+ skipped.[^3]
263
+
264
+ [*Example 1*:
265
+
266
+ This demonstrates a way to include a library `optional` facility only if
267
+ it is available:
268
+
269
+ ``` cpp
270
+ #if __has_include(<optional>)
271
+ # include <optional>
272
+ # define have_optional 1
273
+ #elif __has_include(<experimental/optional>)
274
+ # include <experimental/optional>
275
+ # define have_optional 1
276
+ # define experimental_optional 1
277
+ #else
278
+ # define have_optional 0
279
+ #endif
280
+ ```
281
+
282
+ — *end example*]
283
 
284
  ## Source file inclusion <a id="cpp.include">[[cpp.include]]</a>
285
 
286
  A `#include` directive shall identify a header or source file that can
287
  be processed by the implementation.
 
326
  (that does not match one of the two previous forms) is permitted. The
327
  preprocessing tokens after `include` in the directive are processed just
328
  as in normal text (i.e., each identifier currently defined as a macro
329
  name is replaced by its replacement list of preprocessing tokens). If
330
  the directive resulting after all replacements does not match one of the
331
+ two previous forms, the behavior is undefined.[^4] The method by which a
332
  sequence of preprocessing tokens between a `<` and a `>` preprocessing
333
  token pair or a pair of `"` characters is combined into a single header
334
  name preprocessing token is *implementation-defined*.
335
 
336
  The implementation shall provide unique mappings for sequences
 
341
 
342
  A `#include` preprocessing directive may appear in a source file that
343
  has been read because of a `#include` directive in another file, up to
344
  an *implementation-defined* nesting limit.
345
 
346
+ [*Note 1*:
347
+
348
  Although an implementation may provide a mechanism for making arbitrary
349
  source files available to the `< >` search, in general programmers
350
  should use the `< >` form for headers provided with the implementation,
351
  and the `" "` form for sources outside the control of the
352
  implementation. For instance:
 
356
  #include <unistd.h>
357
  #include "usefullib.h"
358
  #include "myprog.h"
359
  ```
360
 
361
+ — *end note*]
362
+
363
+ [*Example 1*:
364
+
365
  This illustrates macro-replaced `#include` directives:
366
 
367
  ``` cpp
368
  #if VERSION == 1
369
  #define INCFILE "vers1.h"
 
373
  #define INCFILE "versN.h"
374
  #endif
375
  #include INCFILE
376
  ```
377
 
378
+ — *end example*]
379
+
380
  ## Macro replacement <a id="cpp.replace">[[cpp.replace]]</a>
381
 
382
  Two replacement lists are identical if and only if the preprocessing
383
  tokens in both have the same number, ordering, spelling, and white-space
384
  separation, where all white-space separations are considered identical.
385
 
386
+ An identifier currently defined as an object-like macro (see below) may
 
 
 
 
387
  be redefined by another `#define` preprocessing directive provided that
388
+ the second definition is an object-like macro definition and the two
389
+ replacement lists are identical, otherwise the program is ill-formed.
390
+ Likewise, an identifier currently defined as a function-like macro (see
391
+ below) may be redefined by another `#define` preprocessing directive
392
+ provided that the second definition is a function-like macro definition
393
+ that has the same number and spelling of parameters, and the two
394
+ replacement lists are identical, otherwise the program is ill-formed.
395
 
396
  There shall be white-space between the identifier and the replacement
397
  list in the definition of an object-like macro.
398
 
399
+ If the *identifier-list* in the macro definition does not end with an
400
  ellipsis, the number of arguments (including those arguments consisting
401
  of no preprocessing tokens) in an invocation of a function-like macro
402
  shall equal the number of parameters in the macro definition. Otherwise,
403
  there shall be more arguments in the invocation than there are
404
  parameters in the macro definition (excluding the `...`). There shall
405
  exist a `)` preprocessing token that terminates the invocation.
406
 
407
+ The identifier `__VA_ARGS__` shall occur only in the *replacement-list*
408
+ of a function-like macro that uses the ellipsis notation in the
409
+ parameters.
410
 
411
  A parameter identifier in a function-like macro shall be uniquely
412
  declared within its scope.
413
 
414
  The identifier immediately following the `define` is called the *macro
 
426
  ``` bnf
427
  '# define' identifier replacement-list new-line
428
  ```
429
 
430
  defines an *object-like macro* that causes each subsequent instance of
431
+ the macro name[^5] to be replaced by the replacement list of
432
+ preprocessing tokens that constitute the remainder of the directive.[^6]
433
  The replacement list is then rescanned for more macro names as specified
434
  below.
435
 
436
  A preprocessing directive of the form
437
 
 
459
  matching parentheses forms the list of arguments for the function-like
460
  macro. The individual arguments within the list are separated by comma
461
  preprocessing tokens, but comma preprocessing tokens between matching
462
  inner parentheses do not separate arguments. If there are sequences of
463
  preprocessing tokens within the list of arguments that would otherwise
464
+ act as preprocessing directives,[^7] the behavior is undefined.
465
 
466
  If there is a `...` immediately preceding the `)` in the function-like
467
  macro definition, then the trailing arguments, including any separating
468
  comma preprocessing tokens, are merged to form a single item: the
469
  *variable arguments*. The number of arguments so combined is such that,
 
518
  If, in the replacement list of a function-like macro, a parameter is
519
  immediately preceded or followed by a `##` preprocessing token, the
520
  parameter is replaced by the corresponding argument’s preprocessing
521
  token sequence; however, if an argument consists of no preprocessing
522
  tokens, the parameter is replaced by a placemarker preprocessing token
523
+ instead.[^8]
524
 
525
  For both object-like and function-like macro invocations, before the
526
  replacement list is reexamined for more macro names to replace, each
527
  instance of a `##` preprocessing token in the replacement list (not from
528
  an argument) is deleted and the preceding preprocessing token is
 
533
  token results in the non-placemarker preprocessing token. If the result
534
  is not a valid preprocessing token, the behavior is undefined. The
535
  resulting token is available for further macro replacement. The order of
536
  evaluation of `##` operators is unspecified.
537
 
538
+ [*Example 1*:
539
+
540
  In the following fragment:
541
 
542
  ``` cpp
543
  #define hash_hash # ## #
544
  #define mkstr(a) # a
545
  #define in_between(a) mkstr(a)
546
  #define join(c, d) in_between(c hash_hash d)
547
+ char p[] = join(x, y); // equivalent to char p[] = "x ## y";
 
548
  ```
549
 
550
  The expansion produces, at various stages:
551
 
552
  ``` cpp
 
559
 
560
  In other words, expanding `hash_hash` produces a new token, consisting
561
  of two adjacent sharp signs, but this new token is not the `##`
562
  operator.
563
 
564
+ — *end example*]
565
+
566
  ### Rescanning and further replacement <a id="cpp.rescan">[[cpp.rescan]]</a>
567
 
568
  After all parameters in the replacement list have been substituted and
569
  `#` and `##` processing has taken place, all placemarker preprocessing
570
  tokens are removed. Then the resulting preprocessing token sequence is
 
600
 
601
  causes the specified identifier no longer to be defined as a macro name.
602
  It is ignored if the specified identifier is not currently defined as a
603
  macro name.
604
 
605
+ [*Example 1*:
606
+
607
+ The simplest use of this facility is to define a “manifest constant”, as
608
  in
609
 
610
  ``` cpp
611
  #define TABSIZE 100
612
  int table[TABSIZE];
613
  ```
614
 
615
+ — *end example*]
616
+
617
+ [*Example 2*:
618
+
619
  The following defines a function-like macro whose value is the maximum
620
  of its arguments. It has the advantages of working for any compatible
621
  types of the arguments and of generating in-line code without the
622
  overhead of function calling. It has the disadvantages of evaluating one
623
  or the other of its arguments a second time (including side effects) and
 
629
  ```
630
 
631
  The parentheses ensure that the arguments and the resulting expression
632
  are bound properly.
633
 
634
+ — *end example*]
635
+
636
+ [*Example 3*:
637
+
638
  To illustrate the rules for redefinition and reexamination, the sequence
639
 
640
  ``` cpp
641
  #define x 3
642
  #define f(a) f(x * (a))
 
667
  f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1);
668
  int i[] = { 1, 23, 4, 5, };
669
  char c[2][6] = { "hello", "" };
670
  ```
671
 
672
+ — *end example*]
673
+
674
+ [*Example 4*:
675
+
676
  To illustrate the rules for creating character string literals and
677
  concatenating tokens, the sequence
678
 
679
  ``` cpp
680
  #define str(s) # s
681
  #define xstr(s) str(s)
682
+ #define debug(s, t) printf("x" # s "= %d, x" # t "= %s", \
683
+ x ## s, x ## t)
684
  #define INCFILE(n) vers ## n
685
  #define glue(a, b) a ## b
686
  #define xglue(a, b) glue(a, b)
687
  #define HIGHLOW "hello"
688
  #define LOW LOW ", world"
 
716
  ```
717
 
718
  Space around the `#` and `##` tokens in the macro definition is
719
  optional.
720
 
721
+ — *end example*]
722
+
723
+ [*Example 5*:
724
+
725
  To illustrate the rules for placemarker preprocessing tokens, the
726
  sequence
727
 
728
  ``` cpp
729
  #define t(x,y,z) x ## y ## z
 
736
  ``` cpp
737
  int j[] = { 123, 45, 67, 89,
738
  10, 11, 12, };
739
  ```
740
 
741
+ — *end example*]
742
+
743
+ [*Example 6*:
744
+
745
  To demonstrate the redefinition rules, the following sequence is valid.
746
 
747
  ``` cpp
748
  #define OBJ_LIKE (1-1)
749
  #define OBJ_LIKE /* white space */ (1-1) /* other */
750
  #define FUNC_LIKE(a) ( a )
751
+ #define FUNC_LIKE( a )( /* note the white space */ \
752
+ a /* other stuff on this line
753
  */ )
754
  ```
755
 
756
  But the following redefinitions are invalid:
757
 
 
760
  #define OBJ_LIKE (1 - 1) // different white space
761
  #define FUNC_LIKE(b) ( a ) // different parameter usage
762
  #define FUNC_LIKE(b) ( b ) // different parameter spelling
763
  ```
764
 
765
+ — *end example*]
766
+
767
+ [*Example 7*:
768
+
769
  Finally, to show the variable argument list macro facilities:
770
 
771
  ``` cpp
772
  #define debug(...) fprintf(stderr, __VA_ARGS__)
773
  #define showlist(...) puts(#__VA_ARGS__)
 
785
  fprintf(stderr, "X = %d\n", x);
786
  puts("The first, second, and third items.");
787
  ((x>y) ? puts("x>y") : printf("x is %d but y is %d", x, y));
788
  ```
789
 
790
+ — *end example*]
791
+
792
  ## Line control <a id="cpp.line">[[cpp.line]]</a>
793
 
794
  The string literal of a `#line` directive, if present, shall be a
795
  character string literal.
796
 
 
872
 
873
  ## Predefined macro names <a id="cpp.predefined">[[cpp.predefined]]</a>
874
 
875
  The following macro names shall be defined by the implementation:
876
 
877
+ - **`__cplusplus`**
878
 
879
+ The integer literal `202302L`.[^9]
 
880
 
881
  - **`__DATE__`**
882
 
883
  The date of translation of the source file: a character string literal
884
  of the form `"Mmm dd yyyy"`, where the names of the months are the same
 
888
  shall be supplied.
889
 
890
  - **`__FILE__`**
891
 
892
  The presumed name of the current source file (a character string
893
+ literal). [^10]
894
 
895
  - **`__LINE__`**
896
 
897
  The presumed line number (within the current source file) of the current
898
+ source line (an integer literal). [^11]
 
 
899
 
900
  - **`__STDC_HOSTED__`**
901
 
902
  The integer literal `1` if the implementation is a hosted implementation
903
  or the integer literal `0` if it is not.
904
 
905
+ - **`__STDCPP_DEFAULT_NEW_ALIGNMENT__`**
906
+
907
+ An integer literal of type `std::size_t` whose value is the alignment
908
+ guaranteed by a call to `operator new(std::size_t)` or
909
+ `operator new[](std::size_t)`.
910
+
911
+ [*Note 1*: Larger alignments will be passed to
912
+ `operator new(std::size_t, std::align_val_t)`, etc. (
913
+ [[expr.new]]). — *end note*]
914
+
915
  - **`__TIME__`**
916
 
917
  The time of translation of the source file: a character string literal
918
  of the form `"hh:mm:ss"` as in the time generated by the `asctime`
919
  function. If the time of translation is not available, an
 
984
  resulting sequence of characters is processed through translation phase
985
  3 to produce preprocessing tokens that are executed as if they were the
986
  *pp-tokens* in a pragma directive. The original four preprocessing
987
  tokens in the unary operator expression are removed.
988
 
989
+ [*Example 1*:
990
+
991
  ``` cpp
992
  #pragma listing on "..\listing.dir"
993
  ```
994
 
995
  can also be expressed as:
 
1006
  #define PRAGMA(x) _Pragma(#x)
1007
 
1008
  LISTING( ..\listing.dir )
1009
  ```
1010
 
1011
+ — *end example*]
1012
+
1013
  <!-- Link reference definitions -->
1014
  [basic.stc.dynamic.safety]: basic.md#basic.stc.dynamic.safety
1015
  [cpp]: #cpp
1016
  [cpp.concat]: #cpp.concat
1017
  [cpp.cond]: #cpp.cond
 
1027
  [cpp.scope]: #cpp.scope
1028
  [cpp.stringize]: #cpp.stringize
1029
  [cpp.subst]: #cpp.subst
1030
  [cstdint]: language.md#cstdint
1031
  [expr.const]: expr.md#expr.const
1032
+ [expr.new]: expr.md#expr.new
1033
  [intro.multithread]: intro.md#intro.multithread
1034
  [lex.digraph]: lex.md#lex.digraph
1035
  [lex.name]: lex.md#lex.name
1036
  [lex.phases]: lex.md#lex.phases
1037
  [lex.token]: lex.md#lex.token
1038
  [support.limits]: language.md#support.limits
1039
 
1040
+ [^1]: Thus, preprocessing directives are commonly called “lines”. These
1041
  “lines” have no other syntactic significance, as all white space is
1042
  equivalent except in certain situations during preprocessing (see
1043
  the `#` character string literal creation operator in 
1044
  [[cpp.stringize]], for example).
1045
 
1046
  [^2]: Because the controlling constant expression is evaluated during
1047
  translation phase 4, all identifiers either are or are not macro
1048
  names — there simply are no keywords, enumeration constants, etc.
1049
 
1050
+ [^3]: As indicated by the syntax, a preprocessing token shall not follow
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1051
  a `#else` or `#endif` directive before the terminating new-line
1052
  character. However, comments may appear anywhere in a source file,
1053
  including within a preprocessing directive.
1054
 
1055
+ [^4]: Note that adjacent string literals are not concatenated into a
1056
  single string literal (see the translation phases in 
1057
  [[lex.phases]]); thus, an expansion that results in two string
1058
  literals is an invalid directive.
1059
 
1060
+ [^5]: Since, by macro-replacement time, all character literals and
1061
  string literals are preprocessing tokens, not sequences possibly
1062
  containing identifier-like subsequences (see [[lex.phases]],
1063
  translation phases), they are never scanned for macro names or
1064
  parameters.
1065
 
1066
+ [^6]: An alternative token ([[lex.digraph]]) is not an identifier, even
1067
  when its spelling consists entirely of letters and underscores.
1068
  Therefore it is not possible to define a macro whose name is the
1069
  same as that of an alternative token.
1070
 
1071
+ [^7]: A *conditionally-supported-directive* is a preprocessing directive
1072
+ regardless of whether the implementation supports it.
1073
 
1074
+ [^8]: Placemarker preprocessing tokens do not appear in the syntax
1075
  because they are temporary entities that exist only within
1076
  translation phase 4.
1077
 
1078
+ [^9]: It is intended that future versions of this International Standard
1079
+ will replace the value of this macro with a greater value.
1080
+ Non-conforming compilers should use a value with at most five
1081
+ decimal digits.
1082
 
1083
+ [^10]: The presumed source file name can be changed by the `#line`
1084
+ directive.
1085
+
1086
+ [^11]: The presumed line number can be changed by the `#line` directive.