C++ Logo


Advanced search

Re: [std-proposals] explicit modifier for R-value references

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Wed, 21 Jun 2023 10:13:48 +0100
I'll reply to people in series below.

On Mon, Jun 19, 2023 at 1:11 PM Giuseppe D'Angelowrote:
> > void SetVector( vector<int> &&explicit arg )
> > {
> > static vector<int> vec;
> > vec = arg; // no need for 'move' here
> > }
> This sounds extremely dangerous, because
> void SetVector( vector<int> && explicit arg )
> {
> use(arg); // e.g. print(arg). this is now use(std::move(arg))
> m_vec = arg; // hence this is using a moved-from instance
> }

We could require the compiler to issue a diagnostic here:

<source>: In function 'void SetVector(std::vector<int>&&)':
warning: R-value possibly moved more than once
<source>:3:10 first possible move
   10 | use(arg);
<source>:4:6: second possible move
   6 | m_vec = (arg);

I have the word 'possibly' in the diagnostic message because you could
have code such as:

 void SetVector( vector<int> && explicit arg )
     if ( some_global_boolean )
        use(arg); // e.g. print(arg). this is now use(std::move(arg))

     m_vec = arg; // hence this is using a moved-from instance

On Mon, Jun 19, 2023 at 1:57 PM Ville Voutilainen wrote:
> I completely fail to see what is "explicit" about any of this. It's
> the polar opposite of explicit. Lvalues need to be explicitly
> moved-from, this suggestion makes them implicitly movable when the
> decltype of such an lvalue is an rvalue reference.
> And yes, I get it, I understand that it's using a keyword in order to
> avoid an ambiguity if a name is left out. That doesn't
> mean that 'explicit' is a good choice for such a keyword, or even an
> acceptable choice.

I just took a quick scan down through the list of C++ keywords, and
'explicit' was the only one that even vaguely pointed at what I
wanted. 'implicit' would have been better. Or we could even use a
tripple ampersand (globbed as one token).

    int donkey = 7;
    int &&&monkey = move(donkey);
    Func(monkey); // gets passed as an R-value

On Mon, Jun 19, 2023 at 3:56 PM Edward Catmur via Std-Proposals wrote:
>> Perhaps we could introduce a use(std::nomove(arg)); to use in this case?
> Perhaps just use((arg)) - i.e. adding an extra set of parentheses, by analogy to decltype(id) vs. decltype((id))?

Or maybe something like:
     use( !&&arg );

The "!&&" would be globbed as one token and mean "Turn this R-value
ref into an L-value ref", basically meaning "std::nomove(arg)".

> On Mon, 19 Jun 2023 at 14:04, Arthur O'Dwyer wrote:
> I don't see why you in particular would want this kind of implicit double-move-bug factory in addition to that other safer mechanism.

See what I wrote above about the compiler issuing a diagnostic in the
case of a possible double-move.

On Mon, Jun 19, 2023 at 3:56 PM Edward Catmur via Std-Proposals wrote:
> In this forum, it's appropriate to evaluate proposals on their own merits without emotional
> attachment to any other proposal - including one's own. There may well be things I can
> learn from this by approaching it in a spirit of openness.

Can we frame this somewhere? Perhaps put it as a quote on the webpage
you use to sign up for this mailing list?

There are some backwards-thinking people on here who could really do
with adopting such sentiment.

Received on 2023-06-21 09:14:00