C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Allow std::optional to give pointer to yet-to-be-constructed

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 13 Dec 2022 14:35:00 +0000
On Tue, Dec 13, 2022, Lénárd Szolnoki wrote:
> That's library UB.


What I'm proposing is that there would be another function such as
std::optional<T>::GetAddress which would return a T* irrespective of
whether *this has a value.


On Tue, Dec 13, 2022 at 2:20 PM Giuseppe D'Angelo via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> But then you also need then a way to tell optional "you're now loaded
> with a value that I have externally built into your storage"?


I had not intention of creating the object any other way than by using
std::optional<T>::emplace.
Specifically, I wasn't going to do something like the following:

    Monkey *const p = my_optional_object.GetAddress();

    ::new(p) Monkey;

Instead I would do:

    Monkey *const p = my_optional_object.GetAddress();

    my_optional_object.emplace();

Received on 2022-12-13 14:35:12