https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102286
--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This code from PR 103295 is kinda related, which GCC, EDG and MSVC all accept,
but Clang doesn't:
struct S
{
union {
char buf[8];
char* ptr;
};
unsigned len;
constexpr S(const char* s, unsigned n)
{
char* p;
if (n > 7)
p = ptr = new char[n];
else
p = buf;
for (len = 0; len < n; ++len)
p[len] = s[len];
p[len] = '\0';
}
constexpr ~S()
{
if (len > 7)
delete[] ptr;
}
};
constexpr bool test()
{
S s("test", 4);
return true;
}
static_assert( test() );
In the constructor, buf[len] starts the lifetime of the union member, but
p[len] does not.