Thread: Question on l-values.

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    97

    Question on l-values.

    Reading http://msdn.microsoft.com/library/de....2d.values.asp
    I found this:
    ((i < 3) ? i : j) = 7; // Correct. Conditional operator (? returns an l-value.
    Is that defined in the ANSI C++ Standard or is that some extension made by MS?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Section 5.16, paragraph 4:
    If the second and third operands are lvalues and have the same type, the result is of that type and is an lvalue.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    L-Value = Left-Value

    An L-Value is the "thing" on the left side of an assignment (=) operation.

    X = 5; // X is an L-value

    5 = X; // This will give an error "5 is not an L-value"

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >An L-Value is the "thing" on the left side of an assignment (=) operation.
    How does this fit into your example?
    Code:
    const int x = 5;
    
    x = 10;
    x is still an lvalue but it can't be used on the left side of assignment. You would be better off saying that an lvalue is a thing that could be an object. That way you can account for non-modifiable lvalues as well as register variables all in one fell swoop. The "left hand side of an assignment" description falls flat when you look at arrays and const objects, and the "thing with an address" description fails for register variables. Fun, huh?
    My best code is written with the delete key.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    HA! I was thinking about changing to "generally the thing on the left..." I do believe the "L" is derived from "Left".

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I was thinking about changing to "generally the thing on the left..."
    Get into the habit of giving yourself wiggle room, it'll protect you from people like me.

    >I do believe the "L" is derived from "Left".
    You can't always go by the name since it may not do just what it used to do. That reminds me of a conversion I had recently. It went something like this:

    Prelude: Okay, what's <xxx>?
    Guy: <xxx> is a static.
    Prelude: ...
    Prelude: Okay, what's <xxx>?
    Guy: I told you, it's a static.
    Prelude: Do you have any idea how many different meanings for static there are?
    Guy: Um, one?
    Prelude: Try ten.
    My best code is written with the delete key.

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Is that defined in the ANSI C++ Standard or is that some extension made by MS?
    OK Hulag, you asked for it...

    (Prelude - See footnote 53) from the C Standard.)

    FROM THE C LANGUAGE STANDARD...
    6.3.2.1 Lvalues, arrays, and function designators
    1 An lvalue is an expression with an object type or an incomplete type other than void;53) if an lvalue does not designate an object when it is evaluated, the behavior is undefined. When an object is said to have a particular type, the type is specified by the lvalue used to designate the object. A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.

    2 Except when it is the operand of the sizeof operator, the unary & operator, the ++ operator, the -- operator, or the left operand of the . operator or an assignment operator, an lvalue that does not have array type is converted to the value stored in the designated object (and is no longer an lvalue). If the lvalue has qualified type, the value has the unqualified version of the type of the lvalue; otherwise, the value has the type of the lvalue. If the lvalue has an incomplete type and does not have array type, the behavior is undefined.

    3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the is undefined.

    4 A function designator is an expression that has function type. Except when it is the operand of the sizeof operator54) or the unary & operator, a function designator with type ‘‘function returning type’’ is converted to an expression that has type ‘‘pointer to function returning type’’.

    Forward references: address and indirection operators (6.5.3.2), assignment operators (6.5.16), common definitions <stddef.h> (7.17), initialization (6.7.8), postfix increment and decrement operators (6.5.2.4), prefix increment and decrement operators (6.5.3.1), the sizeof operator (6.5.3.4), structure and union members (6.5.2.3).

    53) The name ‘‘lvalue’’ comes originally from the assignment expression E1 = E2, in which the left operand E1 is required to be a (modifiable) lvalue. It is perhaps better considered as representing an object ‘‘locator value’’. What is sometimes called ‘‘rvalue’’ is in this International Standard described
    as the ‘‘value of an expression’’. An obvious example of an lvalue is an identifier of an object. As a further example, if E is a unary expression that is a pointer to an object, *E is an lvalue that designates the object to which E points.

    54) Because this conversion does not occur, the operand of the sizeof operator remains a function and violates the constraint in 6.5.3.4.
    FROM THE C++ LANGUAGE STANDARD
    3.10 Lvalues and rvalues

    1 Every expression is either an lvalue or an rvalue.

    2 An lvalue refers to an object or function. Some rvalue expressions—those of class or cvqualified class type—also refer to objects.47)

    3 [Note: some builtin operators and function calls yield lvalues. [Example: if E is an expression of pointer type, then *E is an lvalue expression referring to the object or function to which E points. As another example, the function int& f(); yields an lvalue, so the call f() is an lvalue expression. ]

    4. [Note: some builin operators expect lvalue operands. [Example: builtin assignment operators all expect their left hand operands to be lvalues. ] Other builtin operators yield rvalues, and some expect them. [Example: the unary and binary + operators expect rvalue arguments and yield rvalue results. ] The discussion of each builtin operator in clause 5 indicates whether it expects lvalue operands and whether it yieldsan lvalue. ]

    5 The result of calling a function that does not return a reference is an rvalue. User defined operators are functions, and whether such operators expect or yield lvalues is determined by their parameter and return types.

    6 An expression which holds a temporary object resulting from a cast to a nonreference type is an rvalue (this includes the explicit creation of an object using functional notation (5.2.3)).

    7 Whenever an lvalue appears in a context where an rvalue is expected, the lvalue is converted to an rvalue; see 4.1, 4.2, and 4.3.

    8 The discussion of reference initialization in 8.5.3 and of temporaries in 12.2 indicates the behavior of lvalues and rvalues in other significant contexts.

    9 Class rvalues can have cvqualified types; nonclass rvalues always have cvunqualified types. Rvalues shall always have complete types or the void type; in addition to these types, lvalues can also have incomplete types.

    10 An lvalue for an object is necessary in order to modify the object except that an rvalue of class type can also be used to modify its referent under certain circumstances. [Example: a member function called for an object (9.3) can modify the object. ]

    11 Functions cannot be modified, but pointers to functions can be modifiable.

    12 A pointer to an incomplete type can be modifiable. At some point in the program when the pointed to type is complete, the object at which the pointer points can also be modified.

    13 The referent of a constqualified expression shall not be modified (through that expression), except that if it is of class type and has a mutable component, that component can be modified (7.1.5.1).

    14 If an expression can be used to modify the object to which it refers, the expression is called modifiable. A program that attempts to modify an object through a nonmodifiable lvalue or rvalue expression is illformed.

    15 If a program attempts to access the stored value of an object through an lvalue of other than one of the following types the behavior is undefined48):
    — the dynamic type of the object,
    — a cvqualified version of the dynamic type of the object,
    — a type that is the signed or unsigned type corresponding to the dynamic type of the object,
    — a type that is the signed or unsigned type corresponding to a cvqualified version of the dynamic type of
    the object,
    — an aggregate or union type that includes one of the aforementioned types among its members (including, recursively, a member of a subaggregate or contained union),
    — a type that is a (possibly cvqualified) base class type of the dynamic type of the object,
    — a char or unsigned char type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about using returned values
    By pokiepo in forum C Programming
    Replies: 7
    Last Post: 07-02-2008, 12:10 PM
  2. Question about binary trees and files
    By satory in forum C Programming
    Replies: 9
    Last Post: 03-06-2006, 06:28 AM
  3. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  5. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM