Thread: lvalue rvalue discussion

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by George2 View Post
    Hello everyone,


    Just want to confirm and ask for advice here for experts, since I met with some confusions about recent reading.

    Here is my understanding,

    1. const reference is lvalue;
    2. non-const reference is lvalue;
    3. Return of constructor is rvalue.

    All correct?


    thanks in advance,
    George
    1. No
    2. Yes
    3. Constructors don't have return values.

    Someone else can elaborate, I'm outa time.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I believe the return value from a constructor is an lvalue.
    Code:
    class T {};
    
    int main()
    {
    	T t;
    	T() = t;
    }

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


    In your below code, you invoke the assignment operator of the return value of T(), which is a rvalue. Any comments?

    Quote Originally Posted by bithub View Post
    I believe the return value from a constructor is an lvalue.
    Code:
    class T {};
    
    int main()
    {
    	T t;
    	T() = t;
    }

    regards,
    George

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


    1.

    Quote Originally Posted by iMalc View Post
    1. No
    Why reference to const is not a lvalue? I think the rule to judge lvalue and rvalue is whether or not it is addressable/nameable other than whether or no it could be on left side or right side of =.

    2.

    Quote Originally Posted by iMalc View Post
    3. Constructors don't have return values.
    I mean the object instance returned by contructor... Any comments?


    regards,
    George

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by George2 View Post
    Why reference to const is not a lvalue? I think the rule to judge lvalue and rvalue is whether or not it is addressable/nameable other than whether or no it could be on left side or right side of =.
    What you say is true. However, before the const keyword was introduced to C, an lvalue was defined as anything that could appear on the left hand side of an assignment. Since const was added to the language, that notion became a "modifiable lvalue".

    lvalues are values that have addresses, meaning they are variables or dereferenced references to a certain memory location. In other words, it designates (refers to, addresses, allows examination of) an object. A modifiable lvalue is both addressable and assignable. An unmodifiable lvalue is addressable, but not assignable.

    An rvalue is a value that is stored at a specific memory location (but not necessarily addressable). Any lvalue can be an rvalue (or implicitly converted to an rvalue -- meaning its value is fetched). However, not all rvalues are lvalues.

    The rvalue to lvalue conversion essentially allows assignment of the value of the rvalue into an lvalue.
    Last edited by grumpy; 02-29-2008 at 04:59 AM.

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, 02:35 AM
  2. Get lvalue through rvalue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2007, 08:53 AM
  3. Function argument not converted to reference
    By MWAAAHAAA in forum C++ Programming
    Replies: 14
    Last Post: 10-22-2006, 03:24 AM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM