C++ Logo

std-discussion

Advanced search

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

From: Tianjiao Yin <ytj000_at_[hidden]>
Date: Mon, 9 Nov 2026 10:26:59 -0800
On Mon, 9 Nov 2026, Giuseppe D'Angelo wrote:
> Il 09/11/26 04:40, Tianjiao Yin via Std-Discussion ha scritto:
> 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").

Thanks for the instruction and context. I will report it.

> > 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.

I was thinking of using `static_cast<T&>` for `&` overload, and
`static_cast<const T&>` for `&` overload, and so on. Though
`this->operator*()` is a more unified approach.

-- 
Tianjiao

Received on 2026-11-09 18:27:11