C++ Logo

std-discussion

Advanced search

Re: Possible flaw in std::optional<const T>

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Mon, 9 Nov 2026 10:35:30 +0100
Il 09/11/26 04:40, Tianjiao Yin via Std-Discussion ha scritto:
> This seems to be a regression from C++23 since it's ill-formed in C++23.

This seems to be a fallout from the resolutions of
https://cplusplus.github.io/LWG/issue3973
and
https://cplusplus.github.io/LWG/issue4015

The latter changed the exposition-only member to a union to fix `const
optional<T>`, but this regressed `optional<const T>`?

> Is this intended? Is this already reported?

 From a quick glance into
https://cplusplus.github.io/LWG/lwg-active.html , no; so if you don't
mind reporting it, the instructions are on the top of that page ("How to
submit an issue").

> To preserve the C++23's behavior, I think we should cast `val` back to T&, e.g.

Uhm... does this work for the `const &` overload of and_then? In that
overload, `val` is a `const remove_vc_t<T>` lvalue, meaning that for
`optional<int>` you'd be doing `static_cast<int &>(val)` which is
stripping cv-qualifiers...?

An alternative could be to use `this->operator*()` instead of `val`, as
already done in the converting constructors by LWG4015
(see for instance https://eel.is/c++draft/optional.ctor ); this would
need to be done in all places (Mandates / Effects / Returns etc.) of the
monadic operations.



My 2 c,
--
Giuseppe D'Angelo

Received on 2026-11-09 09:35:38