<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - Unexpected constexpr assignment error relating to object lifetime and std::allocator"
href="https://bugs.llvm.org/show_bug.cgi?id=47047">47047</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Unexpected constexpr assignment error relating to object lifetime and std::allocator
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>C++2a
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>pkeir@outlook.com
</td>
</tr>
<tr>
<th>CC</th>
<td>blitzrakete@gmail.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
</td>
</tr></table>
<p>
<div>
<pre>Created <span class=""><a href="attachment.cgi?id=23830" name="attach_23830" title="A C++ file containing the code described.">attachment 23830</a> <a href="attachment.cgi?id=23830&action=edit" title="A C++ file containing the code described.">[details]</a></span>
A C++ file containing the code described.
The C++20 code below (also attached) fails to compile, with clang reporting
that the "static_assert expression is not an integral constant expression". In
relation to `*p = 42;` clang also notes: "assignment to object outside its
lifetime is not allowed in a constant expression". I believe such use of
`std::allocator` is now permitted within a `constexpr` context. As well as
trunk, the error can be observed using stock clang on Ubuntu 20.04, but at
least version 10 of libstdc++ should be used for its `constexpr`
`std::allocator`. The code compiles and runs with GCC.
#include <memory>
constexpr bool ptr_test()
{
std::allocator<int> alloc;
int *p = alloc.allocate(1);
*p = 42;
int i = *p;
alloc.deallocate(p,1);
return i==42;
}
int main(int argc, char *argv[])
{
static_assert(ptr_test());
return ptr_test() ? 0 : -1;
}</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>