TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
[[unlikely]] attribute is a C++20 standard optimization hint that informs the compiler that a specific execution path is statistically improbable. By applying this attribute to a statement or label, developers instruct the compiler’s backend to de-prioritize the associated branch during instruction scheduling, basic block placement, and static branch prediction.
Compiler Behavior
When a compiler encounters[[unlikely]], it typically alters the generated machine code in the following ways:
- Basic Block Placement: The compiler moves the “cold” (unlikely) basic blocks out of the primary linear execution path. This improves instruction cache (i-cache) locality for the “hot” path by preventing the CPU from fetching instructions that will rarely be executed.
- Branch Prediction: The compiler may emit specific branch prediction prefixes (depending on the target architecture) or structure conditional jumps such that the fall-through path aligns with the likely execution flow.
Syntax and Placement
The attribute appertains to the statement or label it immediately precedes. It is most commonly applied to conditional branches (if statements) and switch cases.
Conditional Statements:
When applied to an if or else branch, the attribute must precede the statement block.
else branch directly:
switch block, the attribute appertains to the case or default label.
Technical Constraints
- Non-binding Hint: The attribute is strictly a heuristic hint. The C++ standard does not mandate any specific optimization, and compilers are permitted to ignore it entirely, especially if Profile-Guided Optimization (PGO) data contradicts the attribute.
- Mutually Exclusive: A single statement or label cannot be marked with both
[[likely]]and[[unlikely]]. - Statement-Level Only: The attribute applies to statements and labels, not to expressions or declarations. Applying it to a boolean expression directly (e.g.,
if ([[unlikely]] x > 5)) is syntactically invalid.
Master C++ with Deep Grasping Methodology!Learn More





