C++ Logo

std-proposals

Advanced search

Re: [std-proposals] void std::optional<T>::abandon(void) noexcept

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Tue, 28 Nov 2023 16:19:12 +0300
On 11/28/23 16:08, Frederick Virchanza Gotham via Std-Proposals wrote:
> On Tue, Nov 28, 2023 at 12:34 PM Jonathan Wakely wrote:
>>
>> You can just move the contained value to another object which you don't destroy. This doesn't need to be added to std::optional.
>
>
> Your edict is discriminatory against enthusiasts of unmovable
> user-defined classes:
>
> #include <mutex>
> #include <optional>
>
> std::mutex dummy1;
>
> alignas(std::mutex) char unsigned dummy2[sizeof(std::mutex)];
>
> int main(void)
> {
> std::optional< std::mutex > var;
>
> var.emplace();
>
> dummy1 = std::move( var.value() ); // doesn't work
> ::new(dummy2) std::mutex( std::move(var.value()) ); // doesn't work
> }

Why are you using optional<> to begin with?

If you know your type is non-moveable, and you know you may want to
switch to another object, your solution is to use a pointer to the object.

Received on 2023-11-28 13:19:14