如何在if-else语句中使用C ++ 20的可能/不太可能属性 [英] How to use C++20's likely/unlikely attribute in if-else statement
问题描述
这个问题与C ++ 20的 [[可能]]
/ [[不太可能]]
功能有关,不是编译器定义的宏。
This question is about C++20's [[likely]]
/[[unlikely]]
feature, not compiler-defined macros.
此文档( cppreference )仅举了一个将它们应用于switch-case语句的示例。 这个切换案例可以使用我的编译器(g ++-7.2)完美地编译,因此我认为编译器已经实现了此功能,尽管当前C ++标准中尚未正式引入该功能。
This documents (cppreference) only gave an example on applying them to a switch-case statement. This switch-case example compiles perfectly with my compiler (g++-7.2) so I assume the compiler has implemented this feature, though it's not yet officially introduced in current C++ standards.
但是当我这样使用它们时: if(condition)[[likely]] {...} else {...}
,我收到警告:
But when I use them like this: if (condition) [[likely]] { ... } else { ... }
, I got a warning:
警告:语句开头的属性将被忽略[-Wattributes]
"warning: attributes at the beginning of statement are ignored [-Wattributes]".
那么我应该如何在if-else语句中使用这些属性?
So how should I use these attributes in an if-else statement?
推荐答案
基于 Jacksonville'18 ISO C ++报告语法正确,但似乎尚未实现:
Based on example from Jacksonville’18 ISO C++ Report the syntax is correct, but it seems that it is not implemented yet:
if (a>b) [[likely]] {
10.6.6可能性属性[dcl.attr.likelihood] 草案
这篇关于如何在if-else语句中使用C ++ 20的可能/不太可能属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!