C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Return Value Optimisation whenever you need it (guaranteed elision)

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sat, 12 Aug 2023 21:31:32 +0100
On Saturday, August 12, 2023, Lénárd Szolnoki wrote:

>
> for return Widget w;



Or the following:

    Widget w : return;

I think it should be possible to mark more than one local variable as
'return' so long as it's not possible for them to co-exist. For example:

if ( something )
{
    Widget w(57,2,78) : return;
    w.lock();
    return w;
}
else
{
    Widget w(82,3,66) : return;
    return w;
}

So the only thing that would be a little awkward here is if you have common
code after the if-else block that needs to manipulate the widget. My paper
with the 'nrvo' template function using 'placement new' makes this easy.
But in this case you would need a lambda something like:

Widget Func(void)
{

auto mylambda = [](Widget *const p)
  {
       p->Manipulate();
  };

if ( something )
{
    Widget w(57,2,78) : return;
    w.lock();
    mylambda(&w);
    return w;
}
else
{
    Widget w(82,3,66) : return;
    mylambda(&w);
    return w;
}

}

Received on 2023-08-12 20:31:34