C++20:[[likely]]、[[unlikely]] 和 __builtin_expect 之间的区别?

C++20: difference between [[likely]], [[unlikely]] and __builtin_expect?

初步资料:根据最近ISO C++ Committee Trip Report, the [[ likely ]] and [[ unlikely ]] attributes for conditional branching will be added in C++20 and is available in the newest version of GNU GCC (you can play with it on the online compiler wandbox.org).


问题:是下面的构造

if (cond) [[ likely ]] { ... }

相当于下面的?

if (__builtin_expect(bool(cond), 1)) { ... }

不同编译器之间是否存在任何性能差异或实现的细微差别,人们应该注意这些差异才能有效地使用它?

Is the following construction equivalent to the following one?

有意,是的。


Are there any performance difference or implementation nuances accross compilers that one should be aware of in order to use it effectively?

P0479可以看出,这些属性的行为没有强制性的措辞要求。他们的行为作为 non-normative 注释的一部分被提及,鼓励但不强制遵循这些实现。

回答这个问题的唯一方法是查看编译器的手册。