Date: Wed, 6 Mar 2024 11:36:23 +0100
On Wed, Mar 6, 2024 at 11:24 AM Mingxin Wang via Lib-Ext <
lib-ext_at_[hidden]> wrote:
> 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:
>
__builtin_constant_p
<https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fconstant_005fp>
tells
you that something can be constant folded, it does not match the semantics
of C++ constant expressions (and is generally only meaningful for integers
/ floating point expressions)
>
>
> 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.
>
I think exploring that problem would be useful. But I don't think this is
SG7-related, and I do think that if a solution is explored, it has to be a
language solution.
>
>
> Thanks,
>
> Mingxin
> _______________________________________________
> Lib-Ext mailing list
> Lib-Ext_at_[hidden]
> Subscription: https://lists.isocpp.org/mailman/listinfo.cgi/lib-ext
> Link to this post: http://lists.isocpp.org/lib-ext/2024/03/26885.php
>
lib-ext_at_[hidden]> wrote:
> 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:
>
__builtin_constant_p
<https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fconstant_005fp>
tells
you that something can be constant folded, it does not match the semantics
of C++ constant expressions (and is generally only meaningful for integers
/ floating point expressions)
>
>
> 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.
>
I think exploring that problem would be useful. But I don't think this is
SG7-related, and I do think that if a solution is explored, it has to be a
language solution.
>
>
> Thanks,
>
> Mingxin
> _______________________________________________
> Lib-Ext mailing list
> Lib-Ext_at_[hidden]
> Subscription: https://lists.isocpp.org/mailman/listinfo.cgi/lib-ext
> Link to this post: http://lists.isocpp.org/lib-ext/2024/03/26885.php
>
Received on 2024-03-06 10:36:42