C++ Logo

std-proposals

Advanced search

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

From: Edward Catmur <ecatmur_at_[hidden]>
Date: Tue, 11 Oct 2022 17:17:58 +0100
On Tue, 11 Oct 2022 at 17:16, Ville Voutilainen via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Tue, 11 Oct 2022 at 19:09, Thiago Macieira via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> >
> > On Tuesday, 11 October 2022 04:30:52 PDT Ville Voutilainen via
> Std-Proposals
> > wrote:
> > > void f(int&&) { puts("int"); }
> > > void f(double&&) { puts("double"); }
> > >
> > > so
> > >
> > > int i = 1;
> > > f(i);
> > >
> > > prints "double", because it can't call void f(int&&). Instead, it'll
> > > convert the int to double,
> > > and call void f(double&&).
> >
> > But why can't it convert int to int and then call the int&& overload?
>
> Presumably because that would be even weirder if you have just the
> int&& overload,
> and overloads need to make sense in isolation, not just as members of
> an overload set.
> A reference parameter still means "don't create an object unless you have
> to".
>

Yes. This is https://eel.is/c++draft/over.best.ics#over.ics.ref-3.sentence-1
:

> [...] an implicit conversion sequence cannot be formed if it requires
[...] binding an rvalue reference to an lvalue other than a function lvalue.

Received on 2022-10-11 16:18:11