From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id C8D1A3858D28; Tue, 14 Dec 2021 16:23:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C8D1A3858D28 From: "barry.revzin at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/103712] New: variable is not a constant expression because it is used in its own initializer Date: Tue, 14 Dec 2021 16:23:15 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: barry.revzin at gmail dot com 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: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 14 Dec 2021 16:23:15 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D103712 Bug ID: 103712 Summary: variable is not a constant expression because it is used in its own initializer Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: barry.revzin at gmail dot com Target Milestone: --- Reduced from StackOverflow (https://stackoverflow.com/q/70342678/2069064): struct selfref { selfref* next =3D nullptr; }; struct exec { selfref mem =3D selfref{}; constexpr exec() { mem.next =3D &mem; } }; constexpr exec do_thing() { return exec{}; } constexpr exec ret =3D do_thing(); constexpr selfref* ptr =3D ret.mem.next; I think this should compile - ret.mem.next points to ret.mem, which is okay since ret has static storage duration. And then ptr should be pointing to something that has static storage duration, which is a permitted result. gcc currently rejects with: :19:26: error: the value of 'ret' is not usable in a constant expression 19 | constexpr selfref* ptr =3D ret.mem.next; | ^~~ :18:16: note: 'ret' used in its own initializer 18 | constexpr exec ret =3D do_thing(); | ^~~ This seems vaguely related to CWG 2278, but do_thing() isn't doing copy elision, so I think this should work.=