C++ Logo


Advanced search

Re: [std-proposals] Specify that argument to function cannot be an rvalue

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Tue, 11 Oct 2022 14:30:52 +0300
On Tue, 11 Oct 2022 at 13:48, Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
> On Tue, Oct 11, 2022 at 10:25 AM Ville Voutilainen
> <ville.voutilainen_at_[hidden]> wrote:
> > It's passing lvalues to an overload set where all parameters are
> > rvalue references,
> > so the direct binding won't work, but via conversion, the int can be
> > converted to a double,
> > and the double to an int, and since the result of that conversion is
> > an rvalue, the rvalue
> > reference can then bind to it.
> The part I don't understand is why it prints "double int" instead of
> "int double".

Let me try again. We have

void f(int&&) { puts("int"); }
void f(double&&) { puts("double"); }


int i = 1;

prints "double", because it can't call void f(int&&). Instead, it'll
convert the int to double,
and call void f(double&&). Similarly for the double variable, it can't
call void f(double&&),
it'll convert the double to int, and then call void f(int&&).

To phrase it differently: the int argument is close enough to int&&
that it doesn't try to perform
further type conversions, but the value category is wrong, so the
overload doesn't match the call.
Then, the other overload is considered, and there a conversion to
double exists, so that conversion
is performed and the rvalue reference to double parameter is then
bound to the result of the conversion.

Received on 2022-10-11 11:31:04