<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/61562>61562</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Destroying an object during a call to construct_at erroneously rejected during constant evaluation
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
davidstone
</td>
</tr>
</table>
<pre>
The following valid (I think) translation unit
```cpp
#include <memory>
struct ref_holder {
constexpr explicit ref_holder(int & value):
m_value(value)
{
}
constexpr operator int() const {
using T = int;
m_value.~T();
return int();
}
private:
int & m_value;
};
constexpr bool f() {
int x = 0;
std::construct_at(&x, ref_holder(x));
return true;
}
static_assert(f());
```
is rejected by clang with
```console
<source>:25:15: error: static assertion expression is not an integral constant expression
static_assert(f());
^~~
<source>:11:3: note: destruction of object outside its lifetime is not allowed in a constant expression
m_value.~T();
^
/opt/compiler-explorer/clang-assertions-trunk-20230319/bin/../include/c++/v1/__memory/construct_at.h:38:50: note: in call to '&ref_holder(x)->operator int()'
return ::new (std::__voidify(*__location)) _Tp(std::forward<_Args>(__args)...);
^
<source>:21:2: note: in call to 'construct_at(&x, ref_holder(x))'
std::construct_at(&x, ref_holder(x));
^
<source>:25:15: note: in call to 'f()'
static_assert(f());
^
1 error generated.
Compiler returned: 1
```
See it live: https://godbolt.org/z/T6Wo6rd6d
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVcGOozgQ_RrnYiUCG0g4cEh3GmnP29IekcFF4h3HRrZJd_bQ374qIAnJpFezGhQRhF3l9169KoT3am8ACpK-kHS3EH04WFdIcVLSB2tgUVt5Lt4PQFurtf1QZk9PQitJCdv8QcNBmR-E5TQ4YbwWQVlDe6MCiXYk2k73LBp_TddNbxhXptG9BEr46xGO1p0Jf5sH-eD6JlAHbXWwWoKjZP1y2ZA31vgAn52j8Nlp1aj5TsI2ygRKWIZQeyAsJ3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