C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 28 Nov 2023 13:08:56 +0000
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
    }

Received on 2023-11-28 13:09:11