C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Wed, 23 Aug 2023 16:06:44 +0100
On Wed, Aug 23, 2023 at 3:37 PM Jason McKesson via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Please answer the question that was asked. I asked what the code
> meant. I did not ask "how this could be implemented". Implementations
> do not matter at this point. You need to start with an explanation of
> *what is happening*, not how a compiler will go about making that
> happen


Okay here's what it boils down to:

Classes like 'optional', 'variant' and 'any' have a constructor and
assignment operator that can take an Lvalue argument or an Rvalue
argument. This is adequate for classes that are
uncopiable-but-movable. But this isn't good enough for classes that
are unmovable-and-uncopiable, for example let's say that a function
returns a mutex and we want to put that mutex inside an std::optional.
I keep using the example of a 'mutex' simply because it's right there
in the standard library and everyone's familiar with it. In the real
world though it would make more sense to use a Widget or whatever
(like in Anton's paper on NRVO).

What I'm suggesting here is that we have a new kind of syntax for
defining a constructor and assignment operator to allow us to take a
PRvalue, as well as a new keyword such as '__emplace' so that we can
control when the PRvalue gets generated. When I say 'generate the
PRvalue' here, I mean invoke the function/functor that returns the
class by value.

If we are to be able to put a PRvalue inside an std::optional in
C++26, then there's two ways of going about it:
Strategy 1: Give std::optional a new member function such as
'PRemplace', to which you pass a function pointer to allow it to
invoke the function and put the return value in a pre-allocated
buffer.
Strategy 2: Give us PR-construction and PR-assignment.

I'm leaning toward Strategy 2.

Received on 2023-08-23 15:06:58