From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id B79C03858D38; Mon, 3 Oct 2022 14:48:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B79C03858D38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664808533; bh=craoVRTX+q1V70FkS+W03tDKOQyksrUihBrdJxiUisw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=p4XN1DGvBoYAVJxd5lc6MkwN/5QieHq9m7PD8PT+oClvVMdvPhsksyYW829aHDYAk nH0RowGJwHyt8iInCqcdLo8yGr1HZFUriy4QkWFUYLP4cFi5y49Vfxuu6TTtS13kbr u87sW2GVnC5B6Vh1H3gdsebTyVV3h4BrhUMmEU80= From: "h2+bugs at fsfe dot org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/107104] semantics of __builtin_constant_p within static_assert and return value Date: Mon, 03 Oct 2022 14:48:53 +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: unknown X-Bugzilla-Keywords: accepts-invalid X-Bugzilla-Severity: normal X-Bugzilla-Who: h2+bugs at fsfe dot org 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: 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D107104 Hannes Hauswedell changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |h2+bugs at fsfe dot org --- Comment #1 from Hannes Hauswedell --- It seems that __builtin_constant_p does not indicate whether something *can= be* a constant but whether *it is* a constant. If you evaluate it in a non-const-context, the expression passed as argument may or may not be evaluated at compile-time, so the value of __builtin_constant_p may or may not be 1. This example illustrates the behaviour: ```cpp constexpr void foobar() {} // =E2=86=92 test() returns 0 or 1 //consteval void foobar() {} // =E2=86=92 test() returns 1 #define TEST_EXPR (foobar(), 0) int test() { static_assert(__builtin_constant_p(TEST_EXPR)); return __builtin_constant_p(TEST_EXPR); } ``` To enforce evaluation in a const-context, you can define a macro like this: #define IS_CONSTEXPR(...) std::integral_constant::value=