std::is_constant_evaluated returns true in C++2b in manifestly runtime functions · Issue #55638 · llvm/llvm-project (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

@jzmaddock

Description

@jzmaddock

Consider the code:

#include #include #include

template constexpr Integer bitwise_sqrt(const Integer& x) { // TODO put real code here, doesn't need it for the bug case though return 0; } template constexpr Integer fast_sqrt(const Integer& x) { return static_cast(std::sqrt(static_cast(x))); } template 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:

  1. The issue only occurs when compiling with -std=c++2b.
  2. The issue does not occur prior to clang-14.
  3. The issue does not occur if we use __builtin_is_constant_evaluated() directly.
  4. Reproduced on Ubuntu-22 with ubuntu clang version 14.0.0-1ubuntu1

This is a reduced test case from Boost.Multiprecision BTW.