Skip to content

P1938R3 if consteval #4660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions source/expressions.tex
Original file line number Diff line number Diff line change
Expand Up @@ -7465,9 +7465,15 @@

\pnum
An expression or conversion is in an \defn{immediate function context}
if it is potentially evaluated and
if it is potentially evaluated and either:
\begin{itemize}
\item
its innermost enclosing non-block scope is
a function parameter scope of an immediate function.
a function parameter scope of an immediate function, or
\item
its enclosing statement is enclosed\iref{stmt.pre} by
the \grammarterm{compound-statement} of a consteval if statement\iref{stmt.if}.
\end{itemize}
An expression or conversion is an \defn{immediate invocation}
if it is a potentially-evaluated explicit or implicit invocation of
an immediate function and
Expand Down
1 change: 1 addition & 0 deletions source/preprocessor.tex
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,7 @@
\defnxname{cpp_generic_lambdas} & \tcode{201707L} \\ \rowsep
\defnxname{cpp_guaranteed_copy_elision} & \tcode{201606L} \\ \rowsep
\defnxname{cpp_hex_float} & \tcode{201603L} \\ \rowsep
\defnxname{cpp_if_consteval} & \tcode{202106L} \\ \rowsep
\defnxname{cpp_if_constexpr} & \tcode{201606L} \\ \rowsep
\defnxname{cpp_impl_coroutine} & \tcode{201902L} \\ \rowsep
\defnxname{cpp_impl_destroying_delete} & \tcode{201806L} \\ \rowsep
Expand Down
53 changes: 53 additions & 0 deletions source/statements.tex
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@
\nontermdef{selection-statement}\br
\keyword{if} \opt{\keyword{constexpr}} \terminal{(} \opt{init-statement} condition \terminal{)} statement\br
\keyword{if} \opt{\keyword{constexpr}} \terminal{(} \opt{init-statement} condition \terminal{)} statement \keyword{else} statement\br
\keyword{if} \opt{\terminal{!}} \keyword{consteval} compound-statement\br
\keyword{if} \opt{\terminal{!}} \keyword{consteval} compound-statement \keyword{else} statement\br
\keyword{switch} \terminal{(} \opt{init-statement} condition \terminal{)} statement
\end{bnf}

Expand Down Expand Up @@ -315,6 +317,57 @@
except that the \grammarterm{init-statement} is
in the same scope as the \grammarterm{condition}.

\pnum
An \keyword{if} statement of the form \tcode{\keyword{if} \keyword{consteval}}
is called a \defnadj{consteval if}{statement}.
The \grammarterm{statement}, if any, in a consteval if statement
shall be a \grammarterm{compound-statement}.
\begin{example}
\begin{codeblock}
constexpr void f(bool b) {
if (true)
if consteval { }
else ; // error: not a \grammarterm{compound-statement}; \keyword{else} not associated with outer \keyword{if}
}
\end{codeblock}
\end{example}

\pnum
If a consteval if statement is evaluated in a context
that is manifestly constant-evaluated\iref{expr.const},
the first substatement is executed.
\begin{note}
The first substatement is an immediate function context.
\end{note}
Otherwise, if the \keyword{else} part of the selection statement is present,
then the second substatement is executed.
A \keyword{case} or \keyword{default} label
appearing within such an \keyword{if} statement
shall be associated with a \keyword{switch} statement
within the same \keyword{if} statement.
A label declared in a substatement of a consteval if statement
shall only be referred to by a statement in the same substatement.

\pnum
An \keyword{if} statement of the form
\begin{ncsimplebnf}
\keyword{if} \terminal{!} \keyword{consteval} compound-statement
\end{ncsimplebnf}
is not itself a consteval if statement,
but is equivalent to the consteval if statement
\begin{ncsimplebnf}
\keyword{if} \keyword{consteval} \terminal{\{} \terminal{\}} \keyword{else} compound-statement
\end{ncsimplebnf}
An \keyword{if} statement of the form
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not loving the fact that this sentence doesn't end properly, but that's what's in the paper...

\begin{ncsimplebnf}
\keyword{if} \terminal{!} \keyword{consteval} compound-statement$_1$ \keyword{else} statement$_2$
\end{ncsimplebnf}
is not itself a consteval if statement,
but is equivalent to the consteval if statement
\begin{ncsimplebnf}
\keyword{if} \keyword{consteval} statement$_2$ \keyword{else} compound-statement$_1$
\end{ncsimplebnf}

\rSec2[stmt.switch]{The \tcode{switch} statement}%
\indextext{statement!\idxcode{switch}}

Expand Down
13 changes: 9 additions & 4 deletions source/utilities.tex
Original file line number Diff line number Diff line change
Expand Up @@ -18143,10 +18143,15 @@

\begin{itemdescr}
\pnum
\returns
\tcode{true} if and only if evaluation of the call occurs
within the evaluation of an expression or conversion
that is manifestly constant-evaluated\iref{expr.const}.
\effects
Equivalent to:
\begin{codeblock}
if consteval {
return true;
} else {
return false;
}
\end{codeblock}

\pnum
\begin{example}
Expand Down