Consider the code:
#include <type_traits> #include <cmath> #include <iostream> template <class Integer> constexpr Integer bitwise_sqrt(const Integer& x) { // TODO put real code here, doesn't need it for the bug case though return 0; } template <class Integer> constexpr Integer fast_sqrt(const Integer& x) { return static_cast<Integer>(std::sqrt(static_cast<double>(x))); } template <class Integer> constexpr Integer get_sqrt(const Integer& x) { if(std::is_constant_evaluated()) return bitwise_sqrt(x); return fast_sqrt(x); } int main() { unsigned i; std::cin >> i; i = get_sqrt(i); std::cout << i << std::endl; }
Input comes from std::cin
, I assume we can all agree that in this context get_sqrt()
can not be a constant context. However, std::is_constant_evaluated()
is returning true.
Note that:
-std=c++2b
.__builtin_is_constant_evaluated()
directly.ubuntu clang version 14.0.0-1ubuntu1
This is a reduced test case from Boost.Multiprecision
BTW.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4