forked from SonarOpenCommunity/sonar-cxx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- [if consteval](https://en.cppreference.com/w/cpp/language/if#Consteval_if) [P1938R3](https://wg21.link/P1938R3) - linked SonarOpenCommunity#2536
- Loading branch information
Showing
4 changed files
with
52 additions
and
9 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
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
40 changes: 40 additions & 0 deletions
40
cxx-squid/src/test/resources/parser/own/C++23/if-consteval.cc
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
constexpr bool is_constant_evaluated() noexcept | ||
{ | ||
if consteval { return true; } else { return false; } | ||
} | ||
|
||
constexpr bool is_runtime_evaluated() noexcept | ||
{ | ||
if !consteval { return true; } else { return false; } | ||
} | ||
|
||
consteval std::uint64_t ipow_ct(std::uint64_t base, std::uint8_t exp) | ||
{ | ||
if (!base) return base; | ||
std::uint64_t res{1}; | ||
while (exp) | ||
{ | ||
if (exp & 1) res *= base; | ||
exp /= 2; | ||
base *= base; | ||
} | ||
return res; | ||
} | ||
|
||
constexpr std::uint64_t ipow(std::uint64_t base, std::uint8_t exp) | ||
{ | ||
if consteval // use a compile-time friendly algorithm | ||
{ | ||
return ipow_ct(base, exp); | ||
} | ||
else // use runtime evaluation | ||
{ | ||
return std::pow(base, exp); | ||
} | ||
} | ||
|
||
int main(int, const char* argv[]) | ||
{ | ||
static_assert(ipow(0, 10) == 0 && ipow(2, 10) == 1024); | ||
std::cout << ipow(std::strlen(argv[0]), 3) << '\n'; | ||
} |