Hacker News new | ask | show | jobs
by pjmlp 973 days ago
And even then they should be handled with extreme care, as they can trigger UB if used incorrectly.
3 comments

Compilers have always been making guesses about what the most likely code path is behind the scenes, but it still needs to behave correctly in the case where it was wrong (that will just be the less-optimal code path). All these attributes are doing is helping the compiler know instead of guess what the hot path is. if there is any way to confuse the compiler into giving undefined behavior with hints like this, that's a compiler bug. (not saying compiler bugs don't exist, but are you aware of a specific bug like this)?
Do you have examples? It's not clear from the article how you could have a UB with them.
If I remember correctly, Timur Doumler does some remarks on that sense on his presentation:

"Standard Attributes in C and C++"

https://youtu.be/TDKqAWtvH9c?si=b5quQkMe7XUvBbG7

Could you least give some timestamps? It's nearly two hours.

Anyway, while it is possible that some attributes can cause UB if misused, I very much doubt that's possible with [[likely]] and [[unlikely]], as they are just hints for the optimizer, and the optimizer is supposed to preserve semantical guarantees.

Some attributes can cause UB (most obviously [[unreachable]] in a spot that's reachable), but [[likely]] and [[unlikely]] can't.