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: Fri, 11 Aug 2023 14:21:08 +0100
On Fri, Aug 11, 2023 at 2:17 PM Breno GuimarĂ£es <brenorg_at_[hidden]> wrote:
>
> Does it work for cases where there are multiple named objects?
>
> std::mutex foo(bool b)
> {
> std::mutex m1, m2;
> if (b)
> return m1;
> else
> return m2;
> }


Since a mutex can neither be moved nor copied, you would have to do
something like:

void foo2(std::mutex *const p, bool const b)
{
     if ( b )
        ::new(p) std::mutex( ............... );
     else
        ::new(p) std::mutex( ............... );
}

Received on 2023-08-11 13:21:20