|
4029 | 4029 | \end{codeblock}
|
4030 | 4030 | \end{example}
|
4031 | 4031 |
|
| 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 | + |
4032 | 4092 | \rSec2[dcl.attr.unused]{Maybe unused attribute}%
|
4033 | 4093 | \indextext{attribute!maybe unused}
|
4034 | 4094 |
|
|
0 commit comments