[Bug c++/107104] semantics of __builtin_constant_p within static_assert and return value
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107104 Yann Droneaud changed: What|Removed |Added CC||yann at droneaud dot fr --- Comment #3 from Yann Droneaud --- I'm experiencing the same issue: #include void a(int v) { static_assert(__builtin_constant_p(v) && v == -1, "failure"); } GCC 13 and below complains: : In function 'a': :16:43: error: expression in static assertion is not constant 16 | static_assert(__builtin_constant_p(v) && v == -1, "failure"); | ^~ Compiler returned: 1 clang current trunk, eg above 15.0, seems to finally get it right: :16:5: error: static assertion failed due to requirement '__builtin_constant_p(v) && v == -1': failure static_assert(__builtin_constant_p(v) && v == -1, "failure"); ^ ~~ /usr/include/assert.h:143:24: note: expanded from macro 'static_assert' # define static_assert _Static_assert ^ 1 error generated. Compiler returned: 1 see https://godbolt.org/z/KKn6xTG8a
[Bug c++/107104] semantics of __builtin_constant_p within static_assert and return value
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107104 --- Comment #2 from Jonathan Wakely --- (In reply to Hannes Hauswedell from comment #1) > It seems that __builtin_constant_p does not indicate whether something *can > be* a constant but whether *it is* a constant. Correct.
[Bug c++/107104] semantics of __builtin_constant_p within static_assert and return value
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107104 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() {}// → 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::value