public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "h2+bugs at fsfe dot org" <gcc-bugzilla@gcc.gnu.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	[thread overview]
Message-ID: <bug-107104-4-UdydKwzI2r@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-107104-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107104

Hannes Hauswedell <h2+bugs at fsfe dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |h2+bugs at fsfe dot org

--- Comment #1 from Hannes Hauswedell <h2+bugs at fsfe dot org> ---
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() {}    // → test() returns 0 or 1
//consteval void foobar() {}  // → 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<bool,
__builtin_constant_p((__VA_ARGS__, 0))>::value

  reply	other threads:[~2022-10-03 14:48 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-30 16:43 [Bug c++/107104] New: " me at inclyc dot cn
2022-10-03 14:48 ` h2+bugs at fsfe dot org [this message]
2022-10-03 14:54 ` [Bug c++/107104] " redi at gcc dot gnu.org
2022-11-12 12:04 ` yann at droneaud dot fr

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-107104-4-UdydKwzI2r@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).