receiving lvalue as rvalue

Discussion in 'C++' started by Jarek Blakarz, Jan 31, 2013.

  1. 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;
    }
    Jarek Blakarz, Jan 31, 2013
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Jan 31, 2013
    #2
    1. Advertising

  3. Jarek Blakarz

    SG Guest

    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/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
    SG, Jan 31, 2013
    #3
  4. Jarek Blakarz

    Marco Nawijn Guest

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

    Marco
    Marco Nawijn, Feb 2, 2013
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Mantoulidis

    ++x returns lvalue but x++ return rvalue

    Chris Mantoulidis, Dec 28, 2003, in forum: C++
    Replies:
    4
    Views:
    485
    Chris Mantoulidis
    Dec 29, 2003
  2. Gonzalo Aguirre

    rvalue / lvalue operator[]

    Gonzalo Aguirre, Jan 2, 2004, in forum: C++
    Replies:
    4
    Views:
    436
    Ron Natalie
    Jan 2, 2004
  3. Denis Remezov

    lvalue rvalue

    Denis Remezov, Jul 16, 2004, in forum: C++
    Replies:
    12
    Views:
    826
  4. Kavya
    Replies:
    9
    Views:
    493
    Dik T. Winter
    Oct 28, 2006
  5. Juha Nieminen
    Replies:
    13
    Views:
    596
    Edek Pienkowski
    Aug 29, 2012
Loading...

Share This Page