C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Copy-construct, move-construct, and PR-construct

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Wed, 23 Aug 2023 07:47:06 -0800
On Wed, Aug 23, 2023 at 7:39 AM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:

> On Wed, Aug 23, 2023 at 4:10 PM Ville Voutilainen <
> ville.voutilainen_at_[hidden]> wrote:
> >
> > I wonder what happened to the third strategy, which is to pass as the
> > argument of existing emplace an object
> > that will perform the function invocation in its conversion operator
> > to the optional's element type, which then
> > allows doing this without any library or language changes, and has
> > field experience as a solution to this problem.
>
>
> Do you mean something like the following?
> GodBolt: https://godbolt.org/z/PfM1EKaWo
>

Yes, but it can be done much simpler.
https://godbolt.org/z/Gr9sT6Ga9

#include <optional>
#include <mutex>

template<class F>
struct SCSE {
F f_;
operator auto() const { return f_(); }
};

int main(void) {
std::optional<std::mutex> om;
om.emplace(SCSE([] {
return std::mutex();
}));
}

The deficiency with the current state of the world is that even SCSE
doesn't help at all with returning immobile objects; you can't use SCSE to
return a locked mutex, for example.

–Arthur

Received on 2023-08-23 15:47:20