Skip to content

Commit 4e93a29

Browse files
jensmaurertkoeppe
authored andcommitted
P1774R8 Portable assumptions
1 parent a078888 commit 4e93a29

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

source/declarations.tex

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8529,6 +8529,43 @@
85298529
\end{codeblock}
85308530
\end{example}
85318531

8532+
\rSec2[dcl.attr.assume]{Assumption attribute}
8533+
8534+
The \grammarterm{attribute-token} \tcode{assume} may be applied to a null statement;
8535+
such a statement is an \defn{assumption}.
8536+
An \grammarterm{attribute-argument-clause} shall be present and
8537+
shall have the form:
8538+
\begin{ncsimplebnf}
8539+
\terminal{(} conditional-expression \terminal{)}
8540+
\end{ncsimplebnf}
8541+
The expression is contextually converted to \tcode{bool}\iref{conv.general}.
8542+
The expression is not evaluated.
8543+
If the converted expression would evaluate to \tcode{true}
8544+
at the point where the assumption appears,
8545+
the assumption has no effect.
8546+
Otherwise, the behavior is undefined.
8547+
\begin{note}
8548+
The expression is potentially evaluated\iref{basic.def.odr}.
8549+
The use of assumptions is intended to allow implementations
8550+
to analyze the form of the expression and
8551+
deduce information used to optimize the program.
8552+
Implementations are not required to deduce
8553+
any information from any particular assumption.
8554+
\end{note}
8555+
\begin{example}
8556+
\begin{codeblock}
8557+
int divide_by_32(int x) {
8558+
[[assume(x >= 0)]];
8559+
return x/32; // The instructions produced for the division
8560+
// may omit handling of negative values.
8561+
}
8562+
int f(int y) {
8563+
[[assume(++y == 43)]]; // \tcode{y} is not incremented
8564+
return y; // statement may be replaced with \tcode{return 42;}
8565+
}
8566+
\end{codeblock}
8567+
\end{example}
8568+
85328569
\rSec2[dcl.attr.depend]{Carries dependency attribute}%
85338570
\indextext{attribute!carries dependency}
85348571

source/expressions.tex

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7359,7 +7359,8 @@
73597359

73607360
\item
73617361
an operation that would have undefined behavior
7362-
as specified in \ref{intro} through \ref{cpp};
7362+
as specified in \ref{intro} through \ref{cpp},
7363+
excluding \ref{dcl.attr.assume};
73637364
\begin{footnote}
73647365
This includes,
73657366
for example, signed integer overflow\iref{expr.pre}, certain
@@ -7506,12 +7507,28 @@
75067507
a \keyword{goto} statement\iref{stmt.goto}.
75077508
\end{itemize}
75087509

7509-
If $E$ satisfies the constraints of a core constant expression, but
7510-
evaluation of $E$ would evaluate an operation that has undefined behavior
7511-
as specified in \ref{library} through \ref{\lastlibchapter}, or
7512-
an invocation of the \tcode{va_start} macro\iref{cstdarg.syn},
7513-
it is unspecified whether $E$ is a core constant expression.
7514-
7510+
It is unspecified whether $E$ is a core constant expression
7511+
if $E$ satisfies the constraints of a core constant expression, but
7512+
evaluation of $E$ would evaluate
7513+
\begin{itemize}
7514+
\item
7515+
an operation that has undefined behavior
7516+
as specified in \ref{library} through \ref{\lastlibchapter},
7517+
\item
7518+
an invocation of the \tcode{va_start} macro\iref{cstdarg.syn}, or
7519+
\item
7520+
a statement with an assumption\iref{dcl.attr.assume}
7521+
whose converted \grammarterm{conditional-expression},
7522+
if evaluated where the assumption appears,
7523+
would not disqualify $E$ from being a core constant expression and
7524+
would not evaluate to \tcode{true}.
7525+
\begin{note}
7526+
$E$ is not disqualified from being a core constant expression
7527+
if the hypothetical evaluation of
7528+
the converted \grammarterm{conditional-expression}
7529+
would disqualify $E$ from being a core constant expression.
7530+
\end{note}
7531+
\end{itemize}
75157532
\begin{example}
75167533
\begin{codeblock}
75177534
int x; // not constant

0 commit comments

Comments
 (0)