Get lvalue through rvalue

This is a discussion on Get lvalue through rvalue within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I do not know how in the following code, rvalue -- return of X(), could result in a ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    Get lvalue through rvalue

    Hello everyone,


    I do not know how in the following code, rvalue -- return of X(), could result in a lvalue finally and binded to a non-const reference input parameter of function f.

    Any ideas?

    Code:
    struct X {
    
    
    };
    
    void f (X& x) {}
    
    int main()
    {
    	f (X() = X());
    
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    I think that's basically a trick question

    We could change it to:
    Code:
    struct X {
    
    
    };
    
    void f (X& x) {}
    
    int main()
    {
    	f (X().operator=(X()));
    
    	return 0;
    }
    So, the copy assignment operator is invoked, and this can handle an rvalue as its argument. Then, it returns a reference, so f() can handle the reference as it is an lvalue. Yet, right after the function is done, the temporary X object is destroyed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks laserlight,


    Great! *it returns a reference*, you mean assignment operator returns a reference, which is a lvalue?

    1. If yes, how do you know assignment operator returns a reference?
    2. It is a const reference or non-const reference?

    Quote Originally Posted by laserlight View Post
    I think that's basically a trick question

    We could change it to:
    Code:
    struct X {
    
    
    };
    
    void f (X& x) {}
    
    int main()
    {
    	f (X().operator=(X()));
    
    	return 0;
    }
    So, the copy assignment operator is invoked, and this can handle an rvalue as its argument. Then, it returns a reference, so f() can handle the reference as it is an lvalue. Yet, right after the function is done, the temporary X object is destroyed.

    regards,
    George

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If we assume that the purpose of the assignment operator is to ASSIGN A COPY of the current object, it can't really return a const reference, can it? To know what the assignment operator returns, you have to look at the declaration of it. Technically, it doesn't have to return a reference at all, and it could, technically be declared as a const reference - you just can't make use of the assignment operator that way - just like many other nonsense variations that you CAN make - it's perfectly possible to convince the compiler you want to divide by zero for example - it just doesn't give you a sensible result.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Great! *it returns a reference*, you mean assignment operator returns a reference, which is a lvalue?
    Yes.

    1. If yes, how do you know assignment operator returns a reference?
    Because the C++ Standard states:
    If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly.
    The implicitly-declared copy assignment operator for a class X will have the form
    Code:
    X& X::operator=(const X&)
    2. It is a const reference or non-const reference?
    non-const reference, as you can infer from the text of the Standard.

    If we assume that the purpose of the assignment operator is to ASSIGN A COPY of the current object, it can't really return a const reference, can it?
    I do not understand your reasoning. In fact, I had the impression that if not because of the convention enshrined in the standard, returning a const reference would be better to prevent the kind of weird syntax that George2 used in his example. After all, it does not affect operator chaining since the copy assignment operator would take their arguments by const reference anyway.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lvalue vs rvalue casting
    By fallout01 in forum C Programming
    Replies: 5
    Last Post: 07-12-2008, 03:35 AM
  2. lvalue rvalue discussion
    By George2 in forum C++ Programming
    Replies: 18
    Last Post: 03-04-2008, 05:48 AM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 05:33 PM
  4. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 10:23 PM
  5. Help with CString class and operator+ overloading
    By skanxalot in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2003, 11:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21