C++ Logo

std-proposals

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 12:25:00 +0300
On Tue, 11 Oct 2022 at 11:52, Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> On Sun, Oct 9, 2022 at 1:30 PM Arthur O'Dwyer via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> >
> > void f(int&&) { puts("int"); }
> > void f(double&&) { puts("double"); }
> >
> > int main() {
> > int i = 1;
> > double d = 2;
> > f(i); // prints "double"
> > f(d); // prints "int"
> > }
>
>
> Please give me a web link, or a short explanation, of why this code
> behaves the way it does.

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.

Received on 2022-10-11 09:25:12