Skip to content

Commit 8fedee0

Browse files
Merge pull request AnthonyCalandra#98 from aesophor/likely-unlikely
C++20: [[{,un}likely]]: fix the position of attributes in example (AnthonyCalandra#97)
2 parents 17f4bc1 + 76138c2 commit 8fedee0

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

CPP20.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,15 +265,32 @@ for (auto v = std::vector{1, 2, 3}; auto& e : v) {
265265
```
266266

267267
### likely and unlikely attributes
268-
Provides a hint to the optimizer that the labelled statement is likely/unlikely to have its body executed.
268+
Provides a hint to the optimizer that the labelled statement has a high probability of being executed.
269+
```c++
270+
switch (n) {
271+
case 1:
272+
// ...
273+
break;
274+
275+
[[likely]] case 2: // n == 2 is considered to be arbitrarily more
276+
// ... // likely than any other value of n
277+
break;
278+
}
279+
```
280+
281+
If one of the likely/unlikely attributes appears after the right parenthesis of an if-statement,
282+
it indicates that the branch is likely/unlikely to have its substatement (body) executed.
269283
```c++
270284
int random = get_random_number_between_x_and_y(0, 3);
271-
[[likely]] if (random > 0) {
285+
if (random > 0) [[likely]] {
272286
// body of if statement
273287
// ...
274288
}
289+
```
275290

276-
[[unlikely]] while (unlikely_truthy_condition) {
291+
It can also be applied to the substatement (body) of an iteration statement.
292+
```c++
293+
while (unlikely_truthy_condition) [[unlikely]] {
277294
// body of while statement
278295
// ...
279296
}

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,15 +360,32 @@ for (std::vector v{1, 2, 3}; auto& e : v) {
360360
```
361361

362362
### likely and unlikely attributes
363-
Provides a hint to the optimizer that the labelled statement is likely/unlikely to have its body executed.
363+
Provides a hint to the optimizer that the labelled statement has a high probability of being executed.
364+
```c++
365+
switch (n) {
366+
case 1:
367+
// ...
368+
break;
369+
370+
[[likely]] case 2: // n == 2 is considered to be arbitrarily more
371+
// ... // likely than any other value of n
372+
break;
373+
}
374+
```
375+
376+
If one of the likely/unlikely attributes appears after the right parenthesis of an if-statement,
377+
it indicates that the branch is likely/unlikely to have its substatement (body) executed.
364378
```c++
365379
int random = get_random_number_between_x_and_y(0, 3);
366-
[[likely]] if (random > 0) {
380+
if (random > 0) [[likely]] {
367381
// body of if statement
368382
// ...
369383
}
384+
```
370385

371-
[[unlikely]] while (unlikely_truthy_condition) {
386+
It can also be applied to the substatement (body) of an iteration statement.
387+
```c++
388+
while (unlikely_truthy_condition) [[unlikely]] {
372389
// body of while statement
373390
// ...
374391
}

0 commit comments

Comments
 (0)