-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Rewrite the feature checks for `[[assume]]`, `[[likely]]`, and `[[unlikely]]` using the C++20 `__cpp_has_attribute()` macro. Unfortunately some compilers will still _warn_ about the attribute ("it's a C++20 extension!") so the checks also need to _try_ the attribute.
- Loading branch information
Showing
4 changed files
with
42 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
int main(int argc, char **argv) | ||
#if !__has_cpp_attribute(assume) | ||
# error "No support for [[assume]] attribute." | ||
#endif | ||
|
||
#include <iostream> | ||
|
||
|
||
void foo(int i) | ||
{ | ||
[[assume(argv != nullptr)]]; | ||
return argc - 1; | ||
[[assume(i > 0)]]; | ||
std::cout << i << '\n'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,14 @@ | ||
// Test for C++20 [[likely]] and [[unlikely]] attributes. | ||
// C++20: Assume support. | ||
|
||
int main(int argc, char **) | ||
{ | ||
#if __cplusplus < 202002L | ||
deliberately_fail(because, older, C++, standard); | ||
#if !__has_cpp_attribute(likely) | ||
# error "No support for [[likely]] / [[unlikely]] attributes." | ||
#endif | ||
|
||
int x = 0; | ||
if (argc == 1) [[likely]] | ||
x = 0; | ||
int foo(int i) | ||
{ | ||
if (i > 0) [[likely]] | ||
return 100; | ||
else | ||
x = 1; | ||
return x; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters