C++ Logo


Advanced search

Re: Yet another member function for std::map

From: Matthew Woehlke <mwoehlke.floss_at_[hidden]>
Date: Mon, 2 Aug 2021 17:14:15 -0400
On 02/08/2021 10.18, Jason McKesson via Std-Proposals wrote:
> So if you have this:
> ```
> optional<T> t = ...; //some T
> T& t_ref = *t;
> t = ...; //some other T
> ```
> If each of those pieces of code is valid, then `t_ref` is also valid.
> It still refers to the value you assigned to the `t`.

I'm inclined to argue this is just as broken as if you replace
`optional<T&>` with `unique_ptr<T>`.

I'm also half inclined to make it an error to assign a `T` to an
`optional<T&>` and instead require assigning a `T*`. (Which, yes,
suggests that assigning a `nullptr` should be a valid way to disengage
the optional.)

The semantics are a little wonky, but the potential to accidentally
shoot yourself in the foot is somewhat reduced.


Received on 2021-08-02 16:14:19