C++ Logo

std-proposals

Advanced search

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

From: Barry Revzin <barry.revzin_at_[hidden]>
Date: Mon, 19 Jun 2023 13:03:26 -0500
On Mon, Jun 19, 2023 at 7:57 AM Ville Voutilainen via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Mon, 19 Jun 2023 at 14:28, Edward Catmur via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> >> You could use it in function parameters as well:
> >>
> >> void SetVector( vector<int> &&explicit arg )
> >> {
> >> static vector<int> vec;
> >> vec = arg; // no need for 'move' here
> >> }
> >>
> >> Furthermore, when used with a template, it could eradicate the need
> >> for "std::forward" as follows:
> >>
> >> template<typename T>
> >> void SetVector( T &&explicit arg )
> >> {
> >> static T obj;
> >> vec = arg; // no need for 'forward' here
> >> }
> >
> >
> > That sounds feasible, and moderately useful. It wouldn't change the
> type, correct (and especially not the function type)? - it would just
> change the result of the value category computed by
> http://eel.is/c++draft/expr.prim.id#unqual-3.sentence-6 .
> >
> > If you're using it on universal references you'll have to say what it
> means when applied to an lvalue reference as well.
> >
> > Would it be applicable to class data members? Non-type template
> parameters?
>
> 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.
>

+1. You're proposing something to implicitly move...

There is an idea that occasionally gets floated to use the explicit keyword
in a function parameter declaration, but that's to reject any implicit
conversions (i.e. to require an explicit conversion on the call side):

void f(explicit int i);

f(42); // ok
f('A'); // ill-formed
f(static_cast<int>('A')); // ok

Whether or not people think this is a worthwhile language feature, this is
a pretty sensible use of the word explicit and seems like the only
reasonable thing it could mean in this context.

Barry

Received on 2023-06-19 18:03:41