[PATCH] libstdc++: Restrict engaged -> disengaged std::optional fix [PR124910]

Jonathan Wakely jwakely.gcc@gmail.com
Tue Apr 21 06:27:17 GMT 2026


On Mon, 20 Apr 2026, 22:25 Patrick Palka, <ppalka@redhat.com> wrote:

> Tested on x86_64-pc-linux-gnu, does this look OK for trunk?
>

OK

(We could use addressof instead of __addressof as they're equally efficient
nowadays, and I think we could just use & here? But it's fine as is.)


> -- >8 --
>
> It turns out a union without an active member does not violate C++20 core
> constant expression rules and r16-8748 was really just a workaround for
> a front end bug.  The actual underlying problem -- that the constexpr
> evaluator treated an explicitly destroyed union member as still active
> -- has been fixed by r16-8767 which makes this workaround unnecessary
> for GCC.  Rather than remove the workaround, restrict it to Clang which
> seems to have a similar bug.
>
>         PR c++/124910
>
> libstdc++-v3/ChangeLog:
>
>         * include/std/optional (_Optional_payload_base::_M_destroy):
>         Restrict r16-8748 workaround to Clang, and adjust comment.
> ---
>  libstdc++-v3/include/std/optional | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/libstdc++-v3/include/std/optional
> b/libstdc++-v3/include/std/optional
> index 49ba7b6b45dd..b9cc019feff7 100644
> --- a/libstdc++-v3/include/std/optional
> +++ b/libstdc++-v3/include/std/optional
> @@ -321,10 +321,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>        {
>         _M_engaged = false;
>         _M_payload._M_value.~_Stored_type();
> -#if __cpp_lib_optional >= 202106L // full constexpr support
> +#if defined(__clang__) && __cpp_lib_optional >= 202106L // full constexpr
> support
>         if (std::is_constant_evaluated())
> -         // Ensure union _M_payload always has an active member, for sake
> -         // of the core constant expression requirements.
> +         // Work around PR124910 for Clang.
>           std::construct_at(std::__addressof(_M_payload._M_empty));
>  #endif
>        }
> --
> 2.54.0.rc1.54.g60f07c4f5c
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20260421/726f1ea8/attachment.htm>


More information about the Libstdc++ mailing list