[Go] left and right values

Source: Internet
Author: User

Http://www.cnblogs.com/dejavu/archive/2012/09/02/2667640.html#commentform

<C/C++> left and right values, L-value and R-value

It has been assumed that the lvalue is the value that can appear to the left of the assignment expression (the side of an assignment expression),left-value; the right value is right-value.

Today I see a saying that I feel a little bit taste:

The L in L-value refers to location, which indicates addressable. The "L" in Lvalue can is thought of as location.
R in R-value refers to read, which indicates readable. The "R" in Rvalue can is thought of as "read" value.

In order to find the provenance of lvalue terminology, first <the C programming language>, Kernighan and Ritchie wrote:

"An object was a manipulatable region of storage; An lvalue is a expression referring to an object.

...

The name ' Lvalue ' comes from the assignment expression E1 = E2 on which the left operand E1 must is an lvalue expression. "

Then I horns to look at the C99 standard and <the New C standard> The book, looking for some clues:

The name "Lvalue" comes originally from the assignment expression E1 = E2, in which the left operand E1 are required to be A (modifiable) lvalue.
Or left value of assignment. The right value being called the rvalue. This is translator writers ' terminology, the C Standard Committee have adopted.

...

It is perhaps better considered as representing an object "locator value".

...

What is sometimes called "rvalue" are in this international standard described as the "value of an" expression.

There seems to be a lack of a unified and clear definition of the standard. As you can see, the original definition of lvalue is really about the left side of the assignment expression, but a better definition is " something in memory, with a specific location (address)" or " something that points to a certain object. "

In many languages, an object is only allowed to be modified by an expression (Assignment). While the C language is more flexible, it allows you to modify the value of an object through an operator (operator), not just through an expression (such as the + + increment operator). This makes it important to clearly differentiate between the meanings of lvalue and rvalue, such as

  Ten//error! 

Compiler (GCC) Error: Error:non-lvalue in increment (non-lvalue self-increment)

If it is clear that the Lvalue (lvalue) is an identifier or an expression that "has a deterministic address (point to an object)", the right value (rvalue) is just a value and does not necessarily point to an object. That's a simple question:

(a++) expression is the first value of a is returned as the value of the entire expression, and a is increased by 1. Then, the value of the (a++) expression does not point to an object (which does not have a memory address) because it resembles 

{
int temp = A;  A = a +1; return temp;
}

Temp is a temporary value, such as a register, and it does not point to an object (temp does not have an actual address assigned). So the (a++) expression is not an lvalue! After that, + + (a++) is equivalent to an rvalue (non-lvalue) self-increment (++temp) is of course unreasonable!

But, notice! The suffix increment expression return value must be a non-lvalue, but prefix increment expression? The return value of the prefix increment expression is different for C and C + +:

  +////C + + ok,c error!
  (a+=1) +=1; C + + Ok,c error!

For the above code,GCC is an error, and g++ is passed!

The reason is that in C, the prefix increment expression (prefix increment) returns a non-lvalue (C standard 6.5.3.1 The expression ++e is equivalent to (e+=1). And in 6.5.16 an Assignment expression has the value of the "the left operand after the assignment," but was not a lvalue. (This also shows the C language (a=1) = 2, such a conjunction is wrong!)

In C + +, however, the prefix increment expression has been explicitly defined as the return value of an lvalue (C + + standard 5.3.2.1 The value is the newvalue of the operand; it's an lvalue.). So it's no problem to do the first + + or after + + (++a) again. (and the assignment expression return is also an lvalue, so (a=1) =2 with no problem)

Other than that

In C, an expression is either an lvalue or a non-lvalue, there is no rvalue argument, and the right value is not known until C + +.

An lvalue is not necessarily a value that can be modified, such as a const object that is an lvalue, but cannot be modified (non-modifiable lvalue).

The right value can be an lvalue and a non-left value.

Other references:

Http://www.cppblog.com/cc/archive/2012/06/27/7619.html

Http://eetimes.com/discussion/programming-pointers/4023341/Lvalues-and-Rvalues

Http://stackoverflow.com/questions/3572753/difference-between-cs-expression-and-cs-expression

Http://homepage.ntlworld.com/dmjones/cbook1_2.pdf

[Go] left and right values

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.