Hi LEWG and SG7,
I am wondering if there is any paper suggesting facilities to observe whether an expression is a constant or not. Specifically, I found
__builtin_constant_p in GCC and clang, but there does not seem to be much documentation, and the behavior is sometimes weird. For example, in the following case:
struct Foo {
constexpr Foo(bool crash) {
if (crash) { std::terminate(); }
}
};
`Foo{false}` should be a valid constant, but `__builtin_constant_p(Foo{false})` returns `false` in both GCC and clang. In the implementation of
P3086 (I plan to present in Tokyo BTW, please attend if you are interested), we had a hard time writing the concept that can SFINAE-safely inspect whether a user-supplied expression can be evaluated at compile-time, and
eventually came up with the following implementation (link):
template <class Expr>
consteval bool is_consteval(Expr)
{ return requires { typename std::bool_constant<(Expr{}(), false)>; }; }
// Sample usage:
// static_assert(is_consteval([] { Foo{false}; }));
// static_assert(!is_consteval([] { Foo{true}; }));
We feel this can be a handy tool for more users and probably be added to the standard library (or even a language feature like noexcept(expr)?). Since it may also have some overlap with reflection, I am wondering if SG7 experts have more
thoughts.
Thanks,
Mingxin