Skip to content

Commit 00f7bae

Browse files
committed
P0479R5 Likely and unlikely attributes
Editorially add an example as requested by CWG Fixes #1956
1 parent 312c209 commit 00f7bae

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

source/declarations.tex

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4029,6 +4029,66 @@
40294029
\end{codeblock}
40304030
\end{example}
40314031

4032+
\rSec2[dcl.attr.likelihood]{Likelihood attributes}%
4033+
\indextext{attribute!likely}
4034+
\indextext{attribute!unlikely}
4035+
4036+
\pnum
4037+
The \grammarterm{attribute-token}s
4038+
\tcode{likely} and \tcode{unlikely}
4039+
may be applied to labels or statements.
4040+
The \grammarterm{attribute-token}s
4041+
\tcode{likely} and \tcode{unlikely}
4042+
shall appear at most once in each \grammarterm{attribute-list}
4043+
and no \grammarterm{attribute-argument-clause} shall be present.
4044+
The \grammarterm{attribute-token} \tcode{likely}
4045+
shall not appear in an \grammarterm{attribute-specifier-seq}
4046+
that contains the \grammarterm{attribute-token} \tcode{unlikely}.
4047+
4048+
\pnum
4049+
\begin{note}
4050+
The use of the \tcode{likely} attribute
4051+
is intended to allow implementations to optimize for
4052+
the case where paths of execution including it
4053+
are arbitrarily more likely
4054+
than any alternative path of execution
4055+
that does not include such an attribute on a statement or label.
4056+
The use of the \tcode{unlikely} attribute
4057+
is intended to allow implementations to optimize for
4058+
the case where paths of execution including it
4059+
are arbitrarily more unlikely
4060+
than any alternative path of execution
4061+
that does not include such an attribute on a statement or label.
4062+
A path of execution includes a label
4063+
if and only if it contains a jump to that label.
4064+
Excessive usage of either of these attributes
4065+
is liable to result in performance degradation.
4066+
\end{note}
4067+
4068+
\pnum
4069+
\begin{example}
4070+
\begin{codeblock}
4071+
void g(int);
4072+
int f(int n) {
4073+
if (n > 5) [[unlikely]] { // \tcode{n > 5} is considered to be arbitrarily unlikely
4074+
g(0);
4075+
return n * 2 + 1;
4076+
}
4077+
4078+
switch (n) {
4079+
case 1:
4080+
g(1);
4081+
[[fallthrough]];
4082+
4083+
[[likely]] case 2: // \tcode{n == 3} is considered to be arbitrarily more
4084+
g(2); // likely than any other value of \tcode{n}
4085+
break;
4086+
}
4087+
return 3;
4088+
}
4089+
\end{codeblock}
4090+
\end{example}
4091+
40324092
\rSec2[dcl.attr.unused]{Maybe unused attribute}%
40334093
\indextext{attribute!maybe unused}
40344094

0 commit comments

Comments
 (0)