C++ Logo

std-proposals

Advanced search

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

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Sun, 16 Jul 2023 09:13:18 -0400
On Sun, Jul 16, 2023 at 8:48 AM Bjorn Reese via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On 7/16/23 13:54, Frederick Virchanza Gotham via Std-Proposals wrote:
>
> > Emailed him yesterday, nothing back yet. For the time being I will
> > re-write the code to accommodate lambdas-with-captures by using
> > "std::function<void(void*)>" instead of function pointers. When I have
>
> Or you could simply write a mutex wrapper that locks in its constructor.
>

Yes, that would be the "practical programming" approach. :) But it's true
that NRVO-returning a `Wrapper` isn't quite the same thing as
NRVO-returning a `std::mutex` itself.
https://godbolt.org/z/6c54M176G
With that approach, you can never afterward get the mutex "out" of the
Wrapper in order to put it somewhere specific (such as into a
`std::list<std::mutex>`).

I do feel like there ought to be some way to achieve "returning a
non-movable object." Specifically, I think either there ought to be some
clever variation on the "Super Constructing Super Elider
<https://quuxplusone.github.io/blog/2018/05/17/super-elider-round-2/>"
pattern, or (if there's not) it exposes a hole in that pattern: that the
SCSE works *only* to construct types that happen to be movable, and so you
still wouldn't be able to use it to, e.g.,
    std::list<std::mutex> lst;
    lst.emplace_back(my::factory_for_making_locked_mutexes());

I think this is a legitimately interesting "puzzle"; although I don't think
it currently merits any change to the core language nor to the standard
library.

–Arthur

Received on 2023-07-16 13:13:32