C++ Logo

std-proposals

Advanced search

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

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Sun, 9 Oct 2022 08:30:04 -0400
On Sat, Oct 8, 2022 at 5:58 PM Jason McKesson wrote:

> On Sat, Oct 8, 2022 at 5:46 PM Frederick Virchanza Gotham wrote:
> >
> > [...] left it as non-const though, because I didn't want someone to be
> able to do
> > the following:
> >
> > short unsigned k = 7u;
> > Func(k);
> >
> > I didn't want the implicit conversion from rvalue to "reference to
> const".
>

FWIW, I think the status quo is fine (and you've now seen at least one,
maybe two ways of dealing with the problem if you really need to). But as a
data point / fun story, this came up in somebody (else)'s C++ Pub Quiz
recently (maybe CppNorth? I forget the details), and was discussed briefly
on the cpplang Slack.
https://godbolt.org/z/c63hjo4xK

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"
}

Completely unsurprising, when you think about how this overload set needs
to work:
https://godbolt.org/z/4E7hsrqfz

void f(const char *&) { puts("pointer"); }
void f(std::string&&) { puts("string"); }

int main() {
const char *p = "hello";
f(p); // prints "string"
}

–Arthur

Received on 2022-10-09 12:30:16