<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/107668>107668</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Memory allocated via `__builtin_operator_new` never starts its lifetime
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang:frontend,
constexpr
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
tbaederr
</td>
</tr>
</table>
<pre>
This code should work:
```c++
namespace std {
using size_t = decltype(sizeof(0));
template<typename T> struct allocator {
constexpr T *allocate(size_t N) {
return (T*)::operator new(sizeof(T) * N);
}
constexpr void deallocate(void *p) {
::operator delete(p); // both-note {{std::allocator<...>::deallocate' used to delete pointer to object allocated with 'new'}}
}
};
}
constexpr int arrayAlloc() {
int *F = std::allocator<int>().allocate(2);
F[0] = 10;
F[1] = 13;
int Res = F[1] + F[0];
std::allocator<int>().deallocate(F);
return Res;
}
static_assert(arrayAlloc() == 23);
```
https://godbolt.org/z/a57f4vvMf
But the assignment to `F[0]` is currently diagnosed:
```
<source>:23:15: error: static assertion expression is not an integral constant expression
23 | static_assert(arrayAlloc() == 23);
| ^~~~~~~~~~~~~~~~~~
<source>:17:8: note: assignment to object outside its lifetime is not allowed in a constant expression
17 | F[0] = 10;
| ~~~~~^~~~
<source>:23:15: note: in call to 'arrayAlloc()'
23 | static_assert(arrayAlloc() == 23);
| ^~~~~~~~~~~~
```
(I used `::operator new` in the sample to please GCC, but using `__builtin_operator_new` has the same problem.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VU2PozgQ_TWVS2kiYgdIDhxI0lntYfYw6ntkoJJ417GRXaS357C_fWXIB0m3eqSVFiHAVXb5vVflQoWgD5aogHQF6WaiOj46X3ClqCHvJ5Vr3ovXow5Yu4YwHF1nGnxz_i-QJSQbSErIkuGuQazi3VutOlFoVU0YuEHIL2bELmh7wKB_0o4R5AYbqg2_twRiEa1uD2KRgF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