The [[likely]] and [[unlikely]] Attributes

Get a brief introduction to '[[likely]]' and '[[unlikely]]' attributes in C++20.

Proposal P0479R5 for the attributes [[likely]] and [[unlikely]] is the shortest proposal I know of. To give you an idea, below is an interesting note to the proposal.

“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. A path of execution includes a label if and only if it contains a jump to that label. Excessive usage of either of these attributes is liable to result in performance degradation.”

In summary, both attributes allow for giving the optimizer a hint regarding the path of execution expected to be more or less likely.

Get hands-on with 1200+ tech skills courses.