Files
cppdraft_translate/cppdraft/dcl/attr/likelihood.md
2025-10-25 03:02:53 +03:00

60 lines
2.8 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[dcl.attr.likelihood]
# 9 Declarations [[dcl]](./#dcl)
## 9.13 Attributes [[dcl.attr]](dcl.attr#likelihood)
### 9.13.7 Likelihood attributes [dcl.attr.likelihood]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9884)
The [*attribute-token*](dcl.attr.grammar#nt:attribute-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")*s*likely and unlikely may be applied to labels or statements[.](#1.sentence-1)
No [*attribute-argument-clause*](dcl.attr.grammar#nt:attribute-argument-clause "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") shall be present[.](#1.sentence-2)
The [*attribute-token*](dcl.attr.grammar#nt:attribute-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") likely shall not appear in an [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") that contains the [*attribute-token*](dcl.attr.grammar#nt:attribute-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") unlikely[.](#1.sentence-3)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9893)
[*Note [1](#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[.](#2.sentence-1)
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[.](#2.sentence-2)
It is expected that the value of a [*has-attribute-expression*](cpp.cond#nt:has-attribute-expression "15.2Conditional inclusion[cpp.cond]") for the likely and unlikely attributes
is 0 if the implementation does not attempt to use these attributes
for such optimizations[.](#2.sentence-3)
A path of execution includes a label
if and only if it contains a jump to that label[.](#2.sentence-4)
— *end note*]
[*Note [2](#note-2)*:
Excessive usage of either of these attributes
is liable to result in performance degradation[.](#2.sentence-5)
— *end note*]
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9920)
[*Example [1](#example-1)*: void g(int);int f(int n) {if (n > 5) [[unlikely]] { // n > 5 is considered to be arbitrarily unlikely g(0); return n * 2 + 1; }switch (n) {case 1: g(1); [[fallthrough]]; [[likely]] case 2: // n == 2 is considered to be arbitrarily more g(2); // likely than any other value of nbreak; }return 3;} — *end example*]