Skip to content

Commit 44ee8d3

Browse files
committed
P1774R8 Portable assumptions
1 parent d59a4f3 commit 44ee8d3

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
@@ -8589,6 +8589,43 @@
85898589
\end{codeblock}
85908590
\end{example}
85918591

8592+
\rSec2[dcl.attr.assume]{Assumption attribute}
8593+
8594+
The \grammarterm{attribute-token} \tcode{assume} may be applied to a null statement;
8595+
such a statement is an \defn{assumption}.
8596+
An \grammarterm{attribute-argument-clause} shall be present and
8597+
shall have the form:
8598+
\begin{ncsimplebnf}
8599+
\terminal{(} conditional-expression \terminal{)}
8600+
\end{ncsimplebnf}
8601+
The expression is contextually converted to \tcode{bool}\iref{conv.general}.
8602+
The expression is not evaluated.
8603+
If the converted expression would evaluate to \tcode{true}
8604+
at the point where the assumption appears,
8605+
the assumption has no effect.
8606+
Otherwise, the behavior is undefined.
8607+
\begin{note}
8608+
The expression is potentially evaluated\iref{basic.def.odr}.
8609+
The use of assumptions is intended to allow implementations
8610+
to analyze the form of the expression and
8611+
deduce information used to optimize the program.
8612+
Implementations are not required to deduce
8613+
any information from any particular assumption.
8614+
\end{note}
8615+
\begin{example}
8616+
\begin{codeblock}
8617+
int divide_by_32(int x) {
8618+
[[assume(x >= 0)]];
8619+
return x/32; // The instructions produced for the division
8620+
// may omit handling of negative values.
8621+
}
8622+
int f(int y) {
8623+
[[assume(++y == 43)]]; // \tcode{y} is not incremented
8624+
return y; // statement may be replaced with \tcode{return 42;}
8625+
}
8626+
\end{codeblock}
8627+
\end{example}
8628+
85928629
\rSec2[dcl.attr.depend]{Carries dependency attribute}%
85938630
\indextext{attribute!carries dependency}
85948631

source/expressions.tex

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

72497249
\item
72507250
an operation that would have undefined behavior
7251-
as specified in \ref{intro} through \ref{cpp};
7251+
as specified in \ref{intro} through \ref{cpp},
7252+
excluding \ref{dcl.attr.assume};
72527253
\begin{footnote}
72537254
This includes,
72547255
for example, signed integer overflow\iref{expr.pre}, certain
@@ -7404,12 +7405,28 @@
74047405
a \keyword{goto} statement\iref{stmt.goto}.
74057406
\end{itemize}
74067407

7407-
If $E$ satisfies the constraints of a core constant expression, but
7408-
evaluation of $E$ would evaluate an operation that has undefined behavior
7409-
as specified in \ref{library} through \ref{\lastlibchapter}, or
7410-
an invocation of the \tcode{va_start} macro\iref{cstdarg.syn},
7411-
it is unspecified whether $E$ is a core constant expression.
7412-
7408+
It is unspecified whether $E$ is a core constant expression
7409+
if $E$ satisfies the constraints of a core constant expression, but
7410+
evaluation of $E$ would evaluate
7411+
\begin{itemize}
7412+
\item
7413+
an operation that has undefined behavior
7414+
as specified in \ref{library} through \ref{\lastlibchapter},
7415+
\item
7416+
an invocation of the \tcode{va_start} macro\iref{cstdarg.syn}, or
7417+
\item
7418+
a statement with an assumption\iref{dcl.attr.assume}
7419+
whose converted \grammarterm{conditional-expression},
7420+
if evaluated where the assumption appears,
7421+
would not disqualify $E$ from being a core constant expression and
7422+
would not evaluate to \tcode{true}.
7423+
\begin{note}
7424+
$E$ is not disqualified from being a core constant expression
7425+
if the hypothetical evaluation of
7426+
the converted \grammarterm{conditional-expression}
7427+
would disqualify $E$ from being a core constant expression.
7428+
\end{note}
7429+
\end{itemize}
74137430
\begin{example}
74147431
\begin{codeblock}
74157432
int x; // not constant

0 commit comments

Comments
 (0)