receiving lvalue as rvalue

J

Jarek Blakarz

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;
}
 
V

Victor Bazarov

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
 
S

SG

Hello Jarek,

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

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/wg21/docs/papers/2008/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
 
M

Marco Nawijn

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/Goin...12-Scott-Meyers-Universal-References-in-Cpp11

Marco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top