https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113294
Bug ID: 113294
Summary: constexpr error from accessing inactive union member
in basic_string after move assignment
Product: gcc
Version: 14.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: pkeir at outlook dot com
Target Milestone: ---
The C++20 program below fails to compile. A call to `basic_string::clear()` in
the move assignment operator leads to an access of inactive union member
`_M_local_buf`.
#include <string>
constexpr bool string_test1()
{
{
std::string str1; // = "1"; // also a problem when small (less than 16)
std::string str2 = "1234567890123456"; // 16 chars
str1 = std::move(str2);
}
return true;
}
static_assert(string_test1());
Changing `__str._M_data(__str._M_local_buf);` to
`__str._M_data(__str._M_use_local_data());` in basic_string.h seems to fix it,
though I've not had time to test it fully. With this solution, the function
below also remains functional:
constexpr bool string_test2()
{
{
std::string str = "1234567890123456"; // 16 chars
str = std::string{"1234567890123456"}; // rvalue assignment
}
return true;
}