Cppreference and this documentation do not state explicitly that likelihood attributes won't work with a single if statement. Or, I just do not understand what is meant by alternative path of execution. So that's my question, will the attribute, say, [[unlikely]], work in the case below?
if (condition) [[unlikely]] {
do_stuff();
}
Yes, it makes sense. The alternative,
[[likely]], path is the one where the condition isfalse, that is, the path not callingdo_stuff();. That becomes the path it'll try to optimize for.Example:
Assembler with
[[likely]]:With
[[unlikely]](and without attribute at all):Those are two slightly different outcomes and without knowing too much about assembler, I'd say the effect of putting
[[likely]]there is clear. It looks to me that putting[[likely]]there made it inline the function while[[unlikely]]left it as a function call.