From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id BD9A93858410; Wed, 6 Sep 2023 18:18:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BD9A93858410 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1694024328; bh=30FqyI0DQT80szPEZcn4HFv5EPPURBrNbCzUK6nWV9U=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ked6lKYYOm+AkhX/+l4K/UqFgRGKqrRSKq08bKHbWEZMxwaO0JoQPC7YMTJD5la8/ 4gnwQP0gmdZnk23vclEfDvbstOofhg5P58L3Dx+rnyT9Y6dAZ3gZLoe9VHZVFHqTF1 HyhdPQFNMx7/bz99w4QWf1v/uLsBHdhaRAuIFuBA= From: "danakj at orodu dot net" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/110158] Cannot use union with std::string inside in constant expression Date: Wed, 06 Sep 2023 18:18:48 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 13.1.0 X-Bugzilla-Keywords: rejects-valid X-Bugzilla-Severity: normal X-Bugzilla-Who: danakj at orodu dot net X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D110158 --- Comment #6 from danakj at orodu dot net --- In case it is of help, here's an even smaller repro that clang reports the error in libstdc++. For whatever reason gcc does not notice the libstdc++ b= ug here. https://gcc.godbolt.org/z/Phndafeoe ``` #include struct S { constexpr ~S() {} std::string s; }; constexpr std::string foo(const S& s) { return s.s; } static_assert(foo(S("hi")) =3D=3D "hi"); // Fails. static_assert(foo(S("a longer string works")) =3D=3D "a longer string works= "); int main() {} ``` The error: ``` :11:15: error: static assertion expression is not an integral const= ant expression static_assert(foo(S("hi")) =3D=3D "hi"); ^~~~~~~~~~~~~~~~~~~~ /opt/compiler-explorer/gcc-12.2.0/lib/gcc/x86_64-linux-gnu/12.2.0/../../../= ../include/c++/12.2.0/bits/basic_string.h:356:10: note: assignment to member '_M_local_buf' of union with no active member is= not allowed in a constant expression __c =3D _CharT(); ^ /opt/compiler-explorer/gcc-12.2.0/lib/gcc/x86_64-linux-gnu/12.2.0/../../../= ../include/c++/12.2.0/bits/basic_string.tcc:229:4: note: in call to '&S(("hi")).s->_M_use_local_data()' _M_use_local_data(); ^ /opt/compiler-explorer/gcc-12.2.0/lib/gcc/x86_64-linux-gnu/12.2.0/../../../= ../include/c++/12.2.0/bits/basic_string.h:642:2: note: in call to '&S(("hi")).s->_M_construct(&"hi"[0], &"hi"[2], {{}})' _M_construct(__s, __end, forward_iterator_tag()); ^ :11:21: note: in call to 'basic_string(&"hi"[0], std::allocator())' static_assert(foo(S("hi")) =3D=3D "hi"); ^ 1 error generated. ```=