0

Studying for an exam in my programming languages class. Came across this excerpt in the textbook.

"The address of a variable is sometimes called its l-value, because the address is what is required when the name of a variable appears in the left side of an assignment."

I've been studying C++ on my own time for some years outside of college, and at the very least the topic of value categories is complex enough that I've had to review it many times to get it somewhat well down in my head, and still have to pull up the cppreference page to review the newer categories. At the very least this seems like a massive over-simplification. Is this simply false? Or is this use of l-value just something I've never come across?

EDIT: The textbook is on Programming languages in general, not specifically C or C++. Apologies for the confusion.

Lurgypai
  • 31
  • 6
  • No. When you have a pointer holding a mutable address that is dereferenced on the left side of the assignment operator, it is an lvalue. E.g. `int a = 5, *b = &a;` The `"...the address is what is required when the name of a variable appears in the left side of an assignment."` verbiage is specifying that the label on the left side of `=` mus represent a valid mutable address, whether it be a normal variable (i.e. label) or a pointer. – David C. Rankin Apr 08 '21 at 19:07
  • 4
    An lvalue *has* an address, not *is* an address – Woodford Apr 08 '21 at 19:09
  • Handy reading: [What are rvalues, lvalues, xvalues, glvalues, and prvalues?](https://stackoverflow.com/questions/3601602) – user4581301 Apr 08 '21 at 19:15
  • 2
    Is the textbook discussing C++, or programming in general? There are multiple possible meanings of "l-value" (and C++ uses "lvalue", not "l-value"). – Keith Thompson Apr 08 '21 at 19:16
  • @KeithThompson Edited the post to be more clear, thanks for the clarification on "l-value" vs "lvalue" too! – Lurgypai Apr 08 '21 at 20:15

1 Answers1

5

The meaning of "lvalue" or "l-value" has a long and complex history. In some older definitions of the term, that sentence is correct -- but it's not consistent with the way the C++ standard defines it. If your textbook was discussing C++, that passage is questionable. If it was discussing programming in general, it's probably OK.

In C++ (and also in C), an lvalue is not a value. It is a kind of expression, i.e., a construct that can appear in C++ source code. It is the expression itself, not the result of evaluating it.

The definition in the C++11 standard is:

An lvalue (so called, historically, because lvalues could appear on the left-hand side of an assignment expression) designates a function or an object.

That definition is in the context of categorizing expressions.

When the terms "lvalue" and "rvalue" were first invented, they referred to things that can appear on the left ('l') or right ('r') side of an assignment. In the original meaning an expression like x could be "evaluated for its lvalue", meaning that it identifies the object named x, or "evaluated for its rvalue", meaning that it retrieves the value currently stored in x. The C and C++ standards have changed the meanings of the terms so that an "lvalue" is an expression, not the result of evaluating it.

(Having said that, the section of the C++11 standard that discusses all this can be a little vague, and there is wording that could suggest that an lvalue might be the result of evaluating an expression.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631