often used seldom defined terms: lvalue

后端 未结 8 1716
南旧
南旧 2020-11-27 17:25

What is an lvalue?

相关标签:
8条回答
  • 2020-11-27 18:25

    The "L" in lvalue is usually described as standing for "location". An lvalue specifies the location of something; as another answerer pointed out, lvalues can typically have their address taken. This is why numeric literals and non-reference function return values are not lvalues.

    The L used to stand for "left" until const was introduced into C++. Const lvalues cannot appear on the left hand side of an assignment.

    0 讨论(0)
  • 2020-11-27 18:25

    For the C programming language C11 draft n1570 6.3.2.1p1 puts it succinctly:

    An lvalue is an expression (with an object type other than void) that potentially designates an object [...]

    It is as simple as that.

    The Wikipedia definition on the day of writing this answer was almost as good

    [...] a value that points to a storage location, potentially allowing new values to be assigned or value that provide accessible memory address of a variable where data can be written, it is referred as location value.


    Examples of lvalue expressions? Given

    int a, b[1], *c;
    

    we can have the following lvalue expressions: a, b and c each of which definitely designate an object. b[0] designates an object as well, hence it is an lvalue expression. What about the potentiality then? *c is always an lvalue expression, but it does not designate an object if c does not hold a pointer to a valid object. b[1] could semantically designate an object but it doesn't because it accesses the array out of bounds. Both of them are lvalue expressions.

    When an lvalue expression that does not actually designate an object is evaluated, the behaviour is undefined.

    An lvalue expression can be more complex than that, for example:

    ((flag ? foo() : bar())->pointed_to_by_struct_member[42])[0] 
    

    if it compiles, it is an lvalue expression.

    Basically everything that you can apply the & (address-of operator) to in C is an lvalue, except functions, since functions are not objects in C.

    So what does the L then stand for?

    C11 n1570 Footnote 64:

    64) 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. [...]


    Notice that E1 = E2 is not required to compile for E1 to be an lvalue in ISO C. 6.3.2.1p1 continues:

    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.

    It used to be case in languages before C (for example B) that all locator values could appear on the left-hand side of the assignment, hence the naming. This is no longer the case in C since arrays could never have been assigned to, and ISO C added const.


    P.S. The same footnote explains the related term rvalue like this:

    [...] What is sometimes called rvalue is in this International Standard described as the value of an expression. [...]

    0 讨论(0)
提交回复
热议问题