[PATCH] libstdc++: Implement std::unreachable() for C++23 (P0627R6)

Matthias Kretz m.kretz@gsi.de
Thu Mar 31 15:50:33 GMT 2022


I like it. But I'd like it even more if we could have

#elif defined _UBSAN
    __ubsan_invoke_ub("reached std::unreachable()");

But to my knowledge UBSAN has no hooks for the library like this (yet).

and...

On Thursday, 31 March 2022 17:30:29 CEST Jonathan Wakely via Gcc-patches 
wrote:
> diff --git a/libstdc++-v3/include/std/utility
> b/libstdc++-v3/include/std/utility index 0d7f8954c5a..e5b5212381d 100644
> --- a/libstdc++-v3/include/std/utility
> +++ b/libstdc++-v3/include/std/utility
> @@ -186,6 +186,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>      constexpr underlying_type_t<_Tp>
>      to_underlying(_Tp __value) noexcept
>      { return static_cast<underlying_type_t<_Tp>>(__value); }
> +
> +#define __cpp_lib_unreachable 202202L
> +  [[noreturn,__gnu__::__always_inline__]]
> +  void
> +  unreachable()
> +  {
> +#ifdef _GLIBCXX_DEBUG
> +    std::__glibcxx_assert_fail("<utility>", 0, "std::unreachable()",
> +                              "inconceivable!");

Funny message, but it should be more helpful, IMHO. :)

-Matthias

> +#elif defined _GLIBCXX_ASSERTIONS
> +    __builtin_trap();
> +#else
> +    __builtin_unreachable();
> +#endif
> +  }


-- 
──────────────────────────────────────────────────────────────────────────
 Dr. Matthias Kretz                           https://mattkretz.github.io
 GSI Helmholtz Centre for Heavy Ion Research               https://gsi.de
 stdₓ::simd
──────────────────────────────────────────────────────────────────────────


More information about the Libstdc++ mailing list