From Jason Turner

[cpp.cond]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpx91eaxrv/{from.md → to.md} +123 -35
tmp/tmpx91eaxrv/{from.md → to.md} RENAMED
@@ -1,26 +1,61 @@
1
  ## Conditional inclusion <a id="cpp.cond">[[cpp.cond]]</a>
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  The expression that controls conditional inclusion shall be an integral
4
  constant expression except that identifiers (including those lexically
5
  identical to keywords) are interpreted as described below[^2] and it may
6
- contain unary operator expressions of the form
 
7
 
8
- ``` bnf
9
- 'defined' identifier
10
- ```
 
 
11
 
12
- or
 
 
13
 
14
- ``` bnf
15
- 'defined (' identifier ')'
16
- ```
 
 
 
 
 
17
 
18
- which evaluate to `1` if the identifier is currently defined as a macro
19
- name (that is, if it is predefined or if it has been the subject of a
20
- `#define` preprocessing directive without an intervening `#undef`
21
- directive with the same subject identifier), `0` if it is not.
22
 
23
  Each preprocessing token that remains (in the list of preprocessing
24
  tokens that will become the controlling expression) after all macro
25
  replacements have occurred shall be in the lexical form of a token (
26
  [[lex.token]]).
@@ -33,29 +68,60 @@ Prior to evaluation, macro invocations in the list of preprocessing
33
  tokens that will become the controlling constant expression are replaced
34
  (except for those macro names modified by the `defined` unary operator),
35
  just as in normal text. If the token `defined` is generated as a result
36
  of this replacement process or use of the `defined` unary operator does
37
  not match one of the two specified forms prior to macro replacement, the
38
- behavior is undefined. After all replacements due to macro expansion and
39
- the `defined` unary operator have been performed, all remaining
40
- identifiers and keywords[^3], except for `true` and `false`, are
41
- replaced with the pp-number `0`, and then each preprocessing token is
42
- converted into a token. The resulting tokens comprise the controlling
43
- constant expression which is evaluated according to the rules of 
44
- [[expr.const]] using arithmetic that has at least the ranges specified
45
- in  [[support.limits]]. For the purposes of this token conversion and
46
- evaluation all signed and unsigned integer types act as if they have the
47
- same representation as, respectively, `intmax_t` or `uintmax_t` (
48
- [[cstdint]]).[^4] This includes interpreting character literals, which
49
- may involve converting escape sequences into execution character set
50
- members. Whether the numeric value for these character literals matches
51
- the value obtained when an identical character literal occurs in an
52
- expression (other than within a `#if` or `#elif` directive) is
53
- *implementation-defined*.[^5] Also, whether a single-character character
54
- literal may have a negative value is *implementation-defined*. Each
55
- subexpression with type `bool` is subjected to integral promotion before
56
- processing continues.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  Preprocessing directives of the forms
59
 
60
  check whether the identifier is or is not currently defined as a macro
61
  name. Their conditions are equivalent to `#if` `defined` *identifier*
@@ -65,10 +131,32 @@ Each directive’s condition is checked in order. If it evaluates to false
65
  (zero), the group that it controls is skipped: directives are processed
66
  only through the name that determines the directive in order to keep
67
  track of the level of nested conditionals; the rest of the directives’
68
  preprocessing tokens are ignored, as are the other preprocessing tokens
69
  in the group. Only the first group whose control condition evaluates to
70
- true (nonzero) is processed. If none of the conditions evaluates to
71
- true, and there is a `#else` directive, the group controlled by the
72
- `#else` is processed; lacking a `#else` directive, all the groups until
73
- the `#endif` are skipped.[^6]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
 
1
  ## Conditional inclusion <a id="cpp.cond">[[cpp.cond]]</a>
2
 
3
+ ``` bnf
4
+ defined-macro-expression:
5
+ 'defined' identifier
6
+ 'defined (' identifier ')'
7
+ ```
8
+
9
+ ``` bnf
10
+ h-preprocessing-token:
11
+ any *preprocessing-token* other than '>'
12
+ ```
13
+
14
+ ``` bnf
15
+ h-pp-tokens:
16
+ h-preprocessing-token
17
+ h-pp-tokens h-preprocessing-token
18
+ ```
19
+
20
+ ``` bnf
21
+ has-include-expression:
22
+ '__has_include ( <' h-char-sequence '> )'
23
+ '__has_include ( "' q-char-sequence '" )'
24
+ '__has_include (' string-literal ')'
25
+ '__has_include ( <' h-pp-tokens '> )'
26
+ ```
27
+
28
  The expression that controls conditional inclusion shall be an integral
29
  constant expression except that identifiers (including those lexically
30
  identical to keywords) are interpreted as described below[^2] and it may
31
+ contain zero or more *defined-macro-expression*s and/or
32
+ *has-include-expression*s as unary operator expressions.
33
 
34
+ A *defined-macro-expression* evaluates to `1` if the identifier is
35
+ currently defined as a macro name (that is, if it is predefined or if it
36
+ has been the subject of a `#define` preprocessing directive without an
37
+ intervening `#undef` directive with the same subject identifier), `0` if
38
+ it is not.
39
 
40
+ The third and fourth forms of *has-include-expression* are considered
41
+ only if neither of the first or second forms matches, in which case the
42
+ preprocessing tokens are processed just as in normal text.
43
 
44
+ The header or source file identified by the parenthesized preprocessing
45
+ token sequence in each contained *has-include-expression* is searched
46
+ for as if that preprocessing token sequence were the *pp-tokens* in a
47
+ `#include` directive, except that no further macro expansion is
48
+ performed. If such a directive would not satisfy the syntactic
49
+ requirements of a `#include` directive, the program is ill-formed. The
50
+ *has-include-expression* evaluates to `1` if the search for the source
51
+ file succeeds, and to `0` if the search fails.
52
 
53
+ The `#ifdef` and `#ifndef` directives, and the `defined` conditional
54
+ inclusion operator, shall treat `__has_include` as if it were the name
55
+ of a defined macro. The identifier `__has_include` shall not appear in
56
+ any context not mentioned in this section.
57
 
58
  Each preprocessing token that remains (in the list of preprocessing
59
  tokens that will become the controlling expression) after all macro
60
  replacements have occurred shall be in the lexical form of a token (
61
  [[lex.token]]).
 
68
  tokens that will become the controlling constant expression are replaced
69
  (except for those macro names modified by the `defined` unary operator),
70
  just as in normal text. If the token `defined` is generated as a result
71
  of this replacement process or use of the `defined` unary operator does
72
  not match one of the two specified forms prior to macro replacement, the
73
+ behavior is undefined.
74
+
75
+ After all replacements due to macro expansion and evaluations of
76
+ *defined-macro-expression*s and *has-include-expression*s have been
77
+ performed, all remaining identifiers and keywords, except for `true` and
78
+ `false`, are replaced with the *pp-number* `0`, and then each
79
+ preprocessing token is converted into a token.
80
+
81
+ [*Note 1*: An alternative token ([[lex.digraph]]) is not an
82
+ identifier, even when its spelling consists entirely of letters and
83
+ underscores. Therefore it is not subject to this
84
+ replacement. *end note*]
85
+
86
+ The resulting tokens comprise the controlling constant expression which
87
+ is evaluated according to the rules of  [[expr.const]] using arithmetic
88
+ that has at least the ranges specified in  [[support.limits]]. For the
89
+ purposes of this token conversion and evaluation all signed and unsigned
90
+ integer types act as if they have the same representation as,
91
+ respectively, `intmax_t` or `uintmax_t` ([[cstdint]]).
92
+
93
+ [*Note 2*: Thus on an implementation where
94
+ `std::numeric_limits<int>::max()` is `0x7FFF` and
95
+ `std::numeric_limits<unsigned int>::max()` is `0xFFFF`, the integer
96
+ literal `0x8000` is signed and positive within a `#if` expression even
97
+ though it is unsigned in translation phase 7 (
98
+ [[lex.phases]]). — *end note*]
99
+
100
+ This includes interpreting character literals, which may involve
101
+ converting escape sequences into execution character set members.
102
+ Whether the numeric value for these character literals matches the value
103
+ obtained when an identical character literal occurs in an expression
104
+ (other than within a `#if` or `#elif` directive) is
105
+ *implementation-defined*.
106
+
107
+ [*Note 3*:
108
+
109
+ Thus, the constant expression in the following `#if` directive and `if`
110
+ statement is not guaranteed to evaluate to the same value in these two
111
+ contexts:
112
+
113
+ ``` cpp
114
+ #if 'z' - 'a' == 25
115
+ if ('z' - 'a' == 25)
116
+ ```
117
+
118
+ — *end note*]
119
+
120
+ Also, whether a single-character character literal may have a negative
121
+ value is *implementation-defined*. Each subexpression with type `bool`
122
+ is subjected to integral promotion before processing continues.
123
 
124
  Preprocessing directives of the forms
125
 
126
  check whether the identifier is or is not currently defined as a macro
127
  name. Their conditions are equivalent to `#if` `defined` *identifier*
 
131
  (zero), the group that it controls is skipped: directives are processed
132
  only through the name that determines the directive in order to keep
133
  track of the level of nested conditionals; the rest of the directives’
134
  preprocessing tokens are ignored, as are the other preprocessing tokens
135
  in the group. Only the first group whose control condition evaluates to
136
+ true (nonzero) is processed; any following groups are skipped and their
137
+ controlling directives are processed as if they were in a group that is
138
+ skipped. If none of the conditions evaluates to true, and there is a
139
+ `#else` directive, the group controlled by the `#else` is processed;
140
+ lacking a `#else` directive, all the groups until the `#endif` are
141
+ skipped.[^3]
142
+
143
+ [*Example 1*:
144
+
145
+ This demonstrates a way to include a library `optional` facility only if
146
+ it is available:
147
+
148
+ ``` cpp
149
+ #if __has_include(<optional>)
150
+ # include <optional>
151
+ # define have_optional 1
152
+ #elif __has_include(<experimental/optional>)
153
+ # include <experimental/optional>
154
+ # define have_optional 1
155
+ # define experimental_optional 1
156
+ #else
157
+ # define have_optional 0
158
+ #endif
159
+ ```
160
+
161
+ — *end example*]
162