Re:C++ Attribute: Likely, Unlikely
by pjmlp on Sunday October 22, 2023 @11:42AM
(#37976334)
And even then they should be handled with extreme care, as they can trigger UB if used incorrectly.
[ Parent
| on HN ]
Re:C++ Attribute: Likely, Unlikely
by uglycoyote on Sunday October 22, 2023 @12:30PM
(#37976799)
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++"
Re:C++ Attribute: Likely, Unlikely
by fluoridation on Sunday October 22, 2023 @02:36PM
(#37977894)
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.
Re:C++ Attribute: Likely, Unlikely
by plorkyeran on Sunday October 22, 2023 @06:08PM
(#37979533)
Some attributes can cause UB (most obviously [[unreachable]] in a spot that's reachable), but [[likely]] and [[unlikely]] can't.