From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 6E70D3857349; Fri, 4 Aug 2023 16:16:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6E70D3857349 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691165787; bh=JHM4PPMwRcPcsHGmWyrbA8HVededmEKYo084UtxpRKs=; h=From:To:Subject:Date:In-Reply-To:References:From; b=L8VYv3tTvT7RsyICrd2+H/ubf8Ceaaz5198ZMK+ZnfLy0f4iYlDPcj3mrMpAKyzgm /L/hpgeD1T6J2FpfRB/dmZPiZCPq2Uz8AM6TPAtThbiovlijpnfua4+y/7+9/grwRw T0aBNNXBvtMOR51P1NN7Ve3MV6MwcP48tyuW1yCI= 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: Fri, 04 Aug 2023 16:16:26 +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 #4 from danakj at orodu dot net --- Here's a repro without the std::string inside a union. It is the SSO union inside the string that causes the error. https://gcc.godbolt.org/z/T8oM8vYnq ``` #include template constexpr T fold(T init, I i, S s, F f) { while (true) { if (i =3D=3D s) return init; else init =3D f(std::move(init), *i++); } } constexpr char v[] =3D {'a', 'b', 'c'}; static_assert(fold(std::string(), std::begin(v), std::end(v), [](std::string acc, char v) { acc.push_back(v); return acc; }) =3D=3D "abc"); int main() {} ``` :18:23: error: non-constant condition for static assertion 14 | static_assert(fold(std::string(), std::begin(v), std::end(v), | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 15 | [](std::string acc, char v) { | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 16 | acc.push_back(v); | ~~~~~~~~~~~~~~~~~ 17 | return acc; | ~~~~~~~~~~~ 18 | }) =3D=3D "abc"); | ~~~^~~~~~~~ :18:32: in 'constexpr' expansion of 'fold(T, I, S, F) [with T =3D std::__cxx11::basic_string; I =3D const char*; S =3D const char*; F = =3D ](std::begin(v), std::end(v), ((), ()))' :18:32: in 'constexpr' expansion of 'std::__cxx11::basic_string((* & std::move<__cxx11::basic_string&>(init)))' :18:23: error: accessing 'std::__cxx11::basic_string::::_M_allocated_capacity' member instead of initialized 'std::__cxx11::basic_string::::_M_local_buf' member in constant expression ASM generation compiler returned: 1=