From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id D628C384780F; Wed, 9 Jun 2021 16:09:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D628C384780F From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/100995] Extend std::is_constant_evaluated in if warning Date: Wed, 09 Jun 2021 16:09:25 +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: 11.0 X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: normal X-Bugzilla-Who: jakub at gcc dot gnu.org X-Bugzilla-Status: ASSIGNED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: mpolacek at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc 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 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: Wed, 09 Jun 2021 16:09:25 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D100995 Jakub Jelinek changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |jakub at gcc dot gnu.org --- Comment #1 from Jakub Jelinek --- namespace std { constexpr inline bool is_constant_evaluated () noexcept { return __builtin_is_constant_evaluated (); } } int foo () { if (std::is_constant_evaluated ()) return 1; return 0; } constexpr int bar () { if (std::is_constant_evaluated ()) return 1; if constexpr (std::is_constant_evaluated ()) return 2; return 0; } consteval int baz () { if (std::is_constant_evaluated ()) return 1; return 0; } In foo, we know it will always evaluate to false, in baz always to true. In non-constexpr functions it will not evaluate everywhere to false, so one would need to leave out e.g. initializers of static variables and the other spots which are manifestly constant evaluated. But say if conditions except for statement expressions in them and if it is= not the init might be always ok and good enough for the warning.=