[Bug c++/107104] semantics of __builtin_constant_p within static_assert and return value
h2+bugs at fsfe dot org
gcc-bugzilla@gcc.gnu.org
Mon Oct 3 14:48:53 GMT 2022
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
More information about the Gcc-bugs
mailing list