Date: Wed, 6 Mar 2024 10:24:06 +0000
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<https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fconstant_005fp> 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<https://wg21.link/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<https://github.com/microsoft/proxy/blob/2.2.1/proxy.h#L51-L53>):
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
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<https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fconstant_005fp> 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<https://wg21.link/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<https://github.com/microsoft/proxy/blob/2.2.1/proxy.h#L51-L53>):
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
Received on 2024-03-06 10:24:11