Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can we allow lvalue reference to bind rvalue?

21 views
Skip to first unread message

michael.po...@gmail.com

unread,
Oct 20, 2015, 2:16:28 PM10/20/15
to
Hi Everyone,

Will anything be broken in C++ if we allow lvalue reference to bind a rvalue (a temporary)?

Then, the following function

void foo(T& t)
{ ... }

may be called in both ways:

T t;
foo(t);
foo(T()); // not a valid C++, but a proposal: allowing calling for temporary

On the other side, if both versions of foo function are defined:

void foo(T& t)
{}
void foo(T&& t)
{}

then different versions of it will be called for lvalue and rvalue arguments, the same way as it is in the current C++.

The advantage of such a modification: we do not need to write both versions of foo() and still we are able to call foo(T&) with any kind of argument! This is good at least for prototyping.

I do not see currently any problem with this idea, but I may miss something here. Any thoughts?

Thanks, Michael

Paavo Helde

unread,
Oct 20, 2015, 2:46:38 PM10/20/15
to
michael.po...@gmail.com wrote in
news:6b1f3f59-bae8-46d2...@googlegroups.com:

> Hi Everyone,
>
> Will anything be broken in C++ if we allow lvalue reference to bind a
> rvalue (a temporary)?

This has been tried and considered to have failed ca 20 years ago. The
canonical example:

void increment(int& x) {
++x;
}

int main() {
long y = 0;
increment(y);
return y;
}

FYI: MSVC used to compile such code for many years, but by now even they
have adhered to the standard.

Cheers
Paavo

michael.po...@gmail.com

unread,
Oct 20, 2015, 2:56:06 PM10/20/15
to
On Tuesday, October 20, 2015 at 2:46:38 PM UTC-4, Paavo Helde wrote:
> michael wrote in
> > Hi Everyone,
> >
> > Will anything be broken in C++ if we allow lvalue reference to bind a
> > rvalue (a temporary)?
>
> This has been tried and considered to have failed ca 20 years ago. The
> canonical example:
>
> void increment(int& x) {
> ++x;
> }
>
> int main() {
> long y = 0;
> increment(y);
> return y;
> }

Yep. Thank you for pointing that out!

Regards,
Michael

0 new messages