https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124910
--- Comment #11 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Patrick Palka <[email protected]>: https://gcc.gnu.org/g:f3e63a9fb3b40f81b794b369a1ab616f01e69730 commit r16-8748-gf3e63a9fb3b40f81b794b369a1ab616f01e69730 Author: Patrick Palka <[email protected]> Date: Sat Apr 18 19:25:59 2026 -0400 libstdc++: Fix constantness of engaged -> disengaged std::optional [PR124910] When an optional that contains a value is cleared, _M_destroy invokes the destructor of the contained value _Storage::_M_value, leaving the union _Storage without an active member. While this is benign at runtime, a union suboject with no active member violates core constant expression requirements and in turn an optional in this state can't be used as a constant initializer, which Clang and recent GCC (since r16-3022) correctly diagnose. To fix this, this patch makes _M_destroy activate the dummy union member _M_empty after destroying _M_value to ensure that the union always has an active member throughout its lifetime. We use std::construct_at instead of simple assignment to work around a front end bug (comment #3 in the PR). Doing so means we don't activate the member in C++17 mode, which should be fine; I don't think it's possible to disengage an engaged optional using only the C++17 constexpr optional operations. PR c++/124910 libstdc++-v3/ChangeLog: * include/std/optional (_Optional_payload_base::_M_destroy) [__cpp_lib_optional >= 202106L]: During constant evaluation, after invoking destructor of _M_value, use construct_at to activate _M_empty. * testsuite/20_util/optional/constexpr/124910.cc: New test. Reviewed-by: Jonathan Wakely <[email protected]>
