Definitions of Lvalue and rvalue in C and C + +

Source: Internet
Author: User

Looking at the GCC documents, see a word lvalue, check powerword its interpretation for the lvalue [meter] left value. Because it is true that the word was heard in the course of the introduction of the principles of compiling, and there was little thought about knowing its meaning. But the length of the GCC document does not understand what is being said. The problem is also in the understanding of what the "left" value of the word "lvalue" means. Look for M-w dictionary, but tell not the word. So Google A, indeed a lot of places are called the left value, I still do not. Finally in a Wikipedia website about site to find the exact interpretation of the word, pick up the following:

Definition:c and C + + have the notion of lvalues and rvalues associated with variables and constants. The rvalue is the data value of the variable, which is, what information it contains. The "R" in Rvalue can is thought of as "read" value. A variable also has an associated lvalue. The "L" in Lvalue can is though of as location, meaning that a variable has a location that data or information can is put Into. This is contrasted with a constant. A constant has some data value, which is a rvalue. But, it cannot be written to. It does not have an lvalue.

Another view of these terms are that objects and a rvalue, namely a variable or a constant can appear on the right hand s IDE of a statement. They have some data value that can is manipulated. Only objects with a lvalue, such as variable, can appear on the left hand side of a statement. An object must is addressable to store a value.

Here are two examples.

int x;
x = 5; // This is fine, 5 is an rvalue, x can be an lvalue.
5 = x; // This is illegal. A literal constant such as 5 is not
    // addressable. It cannot be a lvalue.

This one is very clear. The L in Lvalue refers to the storage address attribute of the value, while the other relative Rvalue value R refers to the Read property, which has nothing to do with the left or right. PowerWord explanation is really a dog excrement ah.

The author's English is also smelly can, oneself did not understand good original text, self-righteous, convinced this mistake, that wrong!

The citation says: "The" R "in Rvalue can is thought of as" read "value."

Is that you can interpret "R" as "read". Did not say is "read" meaning!

In fact, Lvalue, rvalue the original is how to say, I am afraid there is no research. However, called the "Left value", "right value" does not violate the original intention. Because, so far, all computer languages have been assigned to the value of the left side of the assignment, so the title and understanding is very intuitive. The same is true for the amount of assignment.

The reason why there are "location" and "read" is because there are many expressions in C + + that express assignable units, and we cannot simply understand that "lvalue" is a variable. such as: A, *p, * (a->p+1), and so on. These are C/s + + expressions, not variables, so using the meaning of "location" can avoid a lot of misunderstanding. Examples cited by the author are:

5 = x;

Many people can understand it at a glance, but it is not the nature of the problem! Take a look at the following example:

 const int x;
  x = 1;  // 这里 x 是 rvalue! 所以, 这是错误的赋值!
  struct fun {
   int a;
   int& operator()() { return a; }
   int& operator+(const fun& f) { return a+=f.a; }
   int operator-(const fun& f) { return a-f.a; }
  };
  fun f, g1, g2;
  f() = 1;  // 这里 f() 是 lvalue! 所以, 这个赋值是正确的!
  g1 + g2 = 1;// 这里 g1+g2 是 lvalue! 所以, 这个赋值是正确的!
  g1 - g2 = 1;// 这里 g1-g2 是 rvalue! 所以, 这个赋值是错误的!

To understand such examples of the like-minded, it is clear that Lvalue is called "Left value" (ie: l to understand as the left-hand) or what else (such as: l understand into location) is not a matter of principle! After all, in the language of computer programming, location are left!

As for, Rvalue is only relative to lvalue and called "right value" just, also is not a big deal! The author so the wording, I am afraid will also make Lvalue, rvalue the first user to see will be ironic!

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.