[dcl.attr.likelihood] (original) (raw)

9 Declarations [dcl]

9.13 Attributes [dcl.attr]

9.13.7 Likelihood attributes [dcl.attr.likelihood]

The attribute-tokens likely and unlikelymay be applied to labels or statements.

[Note 1:

The use of the likely attribute is intended to allow implementations to optimize for the case where paths of execution including it are arbitrarily more likely than any alternative path of execution that does not include such an attribute on a statement or label.

The use of the unlikely attribute is intended to allow implementations to optimize for the case where paths of execution including it are arbitrarily more unlikely than any alternative path of execution that does not include such an attribute on a statement or label.

It is expected that the value of a has-attribute-expressionfor the likely and unlikely attributes is 0if the implementation does not attempt to use these attributes for such optimizations.

A path of execution includes a label if and only if it contains a jump to that label.

— _end note_]

[Note 2:

Excessive usage of either of these attributes is liable to result in performance degradation.

— _end note_]

[Example 1: void g(int);int f(int n) { if (n > 5) [[unlikely]] { g(0);return n * 2 + 1;} switch (n) { case 1: g(1);[[fallthrough]];[[likely]] case 2: g(2); break;} return 3;} — _end example_]