C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::elide

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 20 May 2024 09:25:23 +0100
On Mon, May 20, 2024 at 7:59 AM Frederick Virchanza Gotham wrote:
>
> Or here's another way of doing it:
> If an exception is thrown and propagates outside of the function,
> then all destructors are invoked in the regular order -- except for
> the destructor of the return object, which is invoked last.


Actually I just had another idea.

------ Step 1: Start off by writing the function you want

mutex Func(void)
{
    mutex m;
    m.lock();
    return m;
}

------ Step 2: Re-write the function to manipulate an std::optional,
we'll call this function an "optifunc":

void Func(optional<mutex> &arg)
{
    arg.emplace();
    arg->lock();
}

------ Step 3: Tell the compiler to take your optifunc and convert it
into a NRVO function

using Func2 = std::nrvo_from_optifunc(Func);

------ Step 4: Now just invoke Func2

int main(void)
{
    mutex m = Func2();
}

Received on 2024-05-20 08:25:37