<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/55638>55638</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
std::is_constant_evaluated returns true in C++2b in manifestly runtime functions
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
jzmaddock
</td>
</tr>
</table>
<pre>
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:
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.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJytVUuP4zYM_jXOhUjgyHlMDj5skl3sHNoFiil6DGSbcbRVpFSiJpn--lKyk3EGs1NsUUCwJfP18SNFV7Z5KTfWeNWgAzog1LbBrPiU5dssvz4Xeb-GH0WhTK1Dg5AVG3o54Y6cVOSz4vN7GvVR0uEHMmU9OZTHV3F6Eh5PWlJnrqX38GgIW3Q3vZqRE15O7iqBStFZedz5vxxl4iEp3MzEAi6ZWPUxlutuAwCZ-MILnr5tv8EpEDAY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