Skip to content

Commit 4bf008f

Browse files
authored
Merge 2021-06 CWG Motion 6
P1401R5 Narrowing contextual conversions to bool
2 parents 915f187 + fd1c475 commit 4bf008f

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

source/declarations.tex

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@
224224

225225
\pnum
226226
\indextext{\idxgram{static_assert}}%
227-
In a \grammarterm{static_assert-declaration}, the
228-
\grammarterm{constant-expression} shall be
229-
a contextually converted constant expression
230-
of type \tcode{bool}\iref{expr.const}.
227+
In a \grammarterm{static_assert-declaration},
228+
the \grammarterm{constant-expression}
229+
is contextually converted to \keyword{bool} and
230+
the converted expression shall be a constant expression\iref{expr.const}.
231231
If the value of the expression when
232232
so converted is \tcode{true}, the declaration has no
233233
effect. Otherwise, the program is ill-formed, and the resulting
@@ -239,6 +239,7 @@
239239
\begin{example}
240240
\begin{codeblock}
241241
static_assert(sizeof(int) == sizeof(void*), "wrong pointer size");
242+
static_assert(sizeof(int[2])); // OK, narrowing allowed
242243
\end{codeblock}
243244
\end{example}
244245

@@ -551,6 +552,14 @@
551552
the function is explicit. Otherwise, the function is not explicit.
552553
A \tcode{(} token that follows \keyword{explicit} is parsed as
553554
part of the \grammarterm{explicit-specifier}.
555+
\begin{example}
556+
\begin{codeblock}
557+
struct S {
558+
explicit(sizeof(char[2])) S(char); // error: narrowing conversion of value 2 to type \keyword{bool}
559+
explicit(sizeof(char)) S(bool); // OK, conversion of value 1 to type \keyword{bool} is non-narrowing
560+
};
561+
\end{codeblock}
562+
\end{example}
554563

555564
\rSec2[dcl.typedef]{The \tcode{typedef} specifier}%
556565
\indextext{specifier!\idxcode{typedef}}

source/exceptions.tex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,12 @@
716716
is
717717
equivalent to the \grammarterm{noexcept-specifier}
718718
\tcode{\keyword{noexcept}(\keyword{true})}.
719+
\begin{example}
720+
\begin{codeblock}
721+
void f() noexcept(sizeof(char[2])); // error: narrowing conversion of value 2 to type \keyword{bool}
722+
void g() noexcept(sizeof(char)); // OK, conversion of value 1 to type \keyword{bool} is non-narrowing
723+
\end{codeblock}
724+
\end{example}
719725

720726
\pnum
721727
If a declaration of a function

source/statements.tex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,23 @@
249249
\end{footnote}
250250

251251
\pnum
252-
If the \keyword{if} statement is of the form \tcode{if constexpr}, the value
253-
of the condition shall be a contextually
254-
converted constant expression of type \tcode{bool}\iref{expr.const}; this
252+
If the \keyword{if} statement is of the form \tcode{if constexpr},
253+
the value of the condition
254+
is contextually converted to \keyword{bool} and
255+
the converted expression shall be a constant expression\iref{expr.const};
256+
this
255257
form is called a \defn{constexpr if} statement. If the value of the
256258
converted condition is \tcode{false}, the first substatement is a
257259
\defn{discarded statement}, otherwise the second substatement, if
258260
present, is a discarded statement. During the instantiation of an
259261
enclosing templated entity\iref{temp.pre}, if the condition is
260262
not value-dependent after its instantiation, the discarded substatement
261263
(if any) is not instantiated.
264+
\begin{example}
265+
\begin{codeblock}
266+
if constexpr (sizeof(int[2])) {} // OK, narrowing allowed
267+
\end{codeblock}
268+
\end{example}
262269
\begin{note}
263270
Odr-uses\iref{basic.def.odr} in a discarded statement do not require
264271
an entity to be defined.

0 commit comments

Comments
 (0)