Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > receiving lvalue as rvalue

Reply
Thread Tools

receiving lvalue as rvalue

 
 
Jarek Blakarz
Guest
Posts: n/a
 
      01-31-2013
Hi

The following program does not compile on g++ version 4.5.4.
It gives the following error message:
cannot bind ‘int’ lvalue to ‘int&&’

I understand why it does not compile.
On the other hand it compiles successfully on the older version of g++.

Today I'm not sure but I think that once I found something on the internet
saying:
"&& accepts both lvalues and rvalues"

My feeling is that "&&" should accept only rvalues.

Please write me what is the correct behaviour.

thanks a lot for explanation.



void fun(int &&arg) {}

int main ( void )
{
int speed = 10;
fun(speed); // compilation error
return 0;
}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-31-2013
On 1/31/2013 6:42 AM, Jarek Blakarz wrote:
> The following program does not compile on g++ version 4.5.4.
> It gives the following error message:
> cannot bind ‘int’ lvalue to ‘int&&’
>
> I understand why it does not compile.
> On the other hand it compiles successfully on the older version of g++.
>
> Today I'm not sure but I think that once I found something on the internet
> saying:
> "&& accepts both lvalues and rvalues"
>
> My feeling is that "&&" should accept only rvalues.
>
> Please write me what is the correct behaviour.
>
> thanks a lot for explanation.
>
>
>
> void fun(int &&arg) {}
>
> int main ( void )
> {
> int speed = 10;
> fun(speed); // compilation error
> return 0;
> }


int&& is a "modifiable r-value reference" and cannot be bound to an
lvalue. The point is to allow manipulation of something that is
imminently going away, like a temporary object that an expression
creates. You could overcome this simply by prepending the name with a plus:

fun(+speed);

The expression '+speed' creates a temporary.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      01-31-2013
Hello Jarek,

let me add some comments in addition to Victor's answer:

On Jan 31, 12:42*pm, Jarek Blakarz wrote:
> The following program does not compile on g++ version 4.5.4.
> It gives the following error message:
> cannot bind ‘int’ lvalue to ‘int&&’
>
> I understand why it does not compile.
> On the other hand it compiles successfully on the older version of g++.
>
> Today I'm not sure but I think that once I found something on the internet
> saying:
> "&& accepts both lvalues and rvalues"


This is outdated information. In earlier drafts of the standard &&
could bind to both lvalues and rvalues. But this was later identified
as a safety problem by some people including Dave Abrahams. See

http://www.open-std.org/jtc1/sc22/wg...008/n2812.html

Now, if you write a function that gets a mutable rvalue reference
parameter you can be sure that this reference either refers to a
temporary object and/or to an object that nobody cares about anymore
and thus can be modified at will.

> void fun(int &&arg) {}
>
> int main ( void )
> {
> * int speed = 10;
> * fun(speed); * * * // compilation error
> * return 0;


This is expected. If you want arg to refer to the speed variable you
can use std::move. But after that you should not rely on any specific
value that speed stores unless the documentation of fun makes any
explicit guarantees about that.

Cheers!
SG
 
Reply With Quote
 
Marco Nawijn
Guest
Posts: n/a
 
      02-02-2013
On Thursday, January 31, 2013 12:42:51 PM UTC+1, Jarek Blakarz wrote:
> Hi
>
>
>
> The following program does not compile on g++ version 4.5.4.
>
> It gives the following error message:
>
> cannot bind ‘int’ lvalue to ‘int&&’
>
>
>
> I understand why it does not compile.
>
> On the other hand it compiles successfully on the older version of g++.
>
>
>
> Today I'm not sure but I think that once I found something on the internet
>
> saying:
>
> "&& accepts both lvalues and rvalues"
>
>
>
> My feeling is that "&&" should accept only rvalues.
>
>
>
> Please write me what is the correct behaviour.
>
>
>
> thanks a lot for explanation.
>
>
>
>
>
>
>
> void fun(int &&arg) {}
>
>
>
> int main ( void )
>
> {
>
> int speed = 10;
>
> fun(speed); // compilation error
>
> return 0;
>
> }

Hi,

Note that it will compile if you change fun(speed) to
fun(std::move(speed)).

There is an excellent video lecture from Scott Meyers
that covers this topic. He introduces a new term
universal reference which makes reasoning about how
T&& binds in different contexts. The link is here
http://channel9.msdn.com/Shows/Going...ences-in-Cpp11

Marco
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Giving an rvalue ref to a function taking an rvalue ref Juha Nieminen C++ 13 08-29-2012 09:25 PM
operators requiring lvalue/rvalue operands and resulting in rvalue/lvalue Kavya C Programming 9 10-28-2006 01:45 AM
lvalue rvalue Denis Remezov C++ 12 10-18-2004 09:11 AM
rvalue / lvalue operator[] Gonzalo Aguirre C++ 4 01-02-2004 06:02 PM
++x returns lvalue but x++ return rvalue Chris Mantoulidis C++ 4 12-29-2003 07:00 AM



Advertisments