Issue |
120846
|
Summary |
[Clang] The assignment to object outside its lifetime is not allowed in a constant _expression_ error while initializing heap allocated memory
|
Labels |
clang
|
Assignees |
|
Reporter |
kridenberg
|
Below I wrote down a simplified version of the code I try to implement in the production environment.
The code below is compiled with the latest GCC and the latest MSVC.
Minimal reproducible example here: [https://godbolt.org/z/za3vKnxzj](url)
```cpp
#include <bit>
#include <cstddef>
#include <memory>
#include <iostream>
struct buffer
{
constexpr buffer(char16_t const* values, size_t const count)
: m_byte_count {count * sizeof(char16_t)}
, m_bytes {std::allocator<std::byte>().allocate(m_byte_count)}
{
struct bytes
{
std::byte bytes[2];
};
for (std::size_t index = 0; index < count; ++index)
{
bytes const bitwise = std::bit_cast<bytes>(values[index]);
m_bytes[(index * sizeof(char16_t)) + 0] = bitwise.bytes[0];
m_bytes[(index * sizeof(char16_t)) + 1] = bitwise.bytes[1];
}
}
constexpr auto compute() const -> int
{
int result = 0;
for (std::size_t index = 0; index < m_byte_count; ++index)
{
result = result ^ static_cast<int>(m_bytes[index]);
}
return result;
}
constexpr ~buffer()
{
std::allocator<std::byte>().deallocate(m_bytes, m_byte_count);
}
std::size_t m_byte_count;
std::byte* m_bytes;
};
consteval auto compute(char16_t const* values, size_t const count) -> int
{
buffer b(values, count);
return b.compute();
}
auto main() -> int
{
constexpr auto value = compute(u"oadpoadopa", 10);
std::cout << value << std::endl;
return 0;
}
```
```
<source>:53:17: error: constexpr variable 'value' must be initialized by a constant _expression_
53 | constexpr auto value = compute(u"oadpoadopa", 10);
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:20:44: note: assignment to object outside its lifetime is not allowed in a constant _expression_
20 | m_bytes[(index * sizeof(char16_t)) + 0] = bitwise.bytes[0];
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
<source>:47:9: note: in call to 'buffer(&u"oadpoadopa"[0], 10)'
47 | buffer b(values, count);
| ^~~~~~~~~~~~~~~~
<source>:53:25: note: in call to 'compute(&u"oadpoadopa"[0], 10)'
53 | constexpr auto value = compute(u"oadpoadopa", 10);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:53:25: error: call to consteval function 'compute' is not a constant _expression_
53 | constexpr auto value = compute(u"oadpoadopa", 10);
| ^
<source>:20:44: note: assignment to object outside its lifetime is not allowed in a constant _expression_
20 | m_bytes[(index * sizeof(char16_t)) + 0] = bitwise.bytes[0];
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
<source>:47:9: note: in call to 'buffer(&u"oadpoadopa"[0], 10)'
47 | buffer b(values, count);
| ^~~~~~~~~~~~~~~~
<source>:53:25: note: in call to 'compute(&u"oadpoadopa"[0], 10)'
53 | constexpr auto value = compute(u"oadpoadopa", 10);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
2 errors generated.
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs