Z
Zach
Can someone please explain what this means and illustrate the
difference with some code.
Thanks,
Zach
difference with some code.
Thanks,
Zach
Zach said:Can someone please explain what this means and illustrate the
difference with some code.
Sure. An l-value is an expression that may legally appear on the left-
hand side of the assignment operator, =.
that may appear on the right-hand side. The set of r-values is a subset
of the the set of l-values. For example, given:
int a;
int b[50];
The following are l-values (and also r-values):
a
b[10]
Whereas the following are ONLY r-values:
17
a + 5
b
Richard Heathfield said:Strictly speaking, b is an lvalue, but not a modifiable lvalue. Section
6.3.2.1(1) - which I quoted above - goes on to say: "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."
Chris said:Sure. An l-value is an expression that may legally appear on the left-
hand side of the assignment operator, =. An r-value is an expression
that may appear on the right-hand side. The set of r-values is a subset
of the the set of l-values. For example, given:
int a;
int b[50];
The following are l-values (and also r-values):
a
b[10]
Whereas the following are ONLY r-values:
17
a + 5
b
Therefore, the following line of code will fail to compile:
a + 5 = 15;
Hope that helps,
Chris said:Interesting... is there a situation in legal code where this makes a
difference?
Chris Smith wrote:
If (b) was not an lvalue,
then (&b) would be undefined.
[...]Richard Heathfield said:Chris Smith said:
More formally, "an lvalue is an expression with an object type or an
incomplete type other than void; if an lvalue does not designate an object
when it is evaluated, the behaviour is undefined."
Chris said:Sure. An l-value is an expression that may legally appear on the left-
hand side of the assignment operator, =. An r-value is an expression
that may appear on the right-hand side. The set of r-values is a subset
of the the set of l-values. For example, given:
int a;
int b[50];
The following are l-values (and also r-values):
a
b[10]
Whereas the following are ONLY r-values:
17
a + 5
b
Therefore, the following line of code will fail to compile:
a + 5 = 15;
Hope that helps,
Where are the rules of what may go on each side?
Chris said:Sure. An l-value is an expression that may legally appear on the left-
hand side of the assignment operator, =. An r-value is an expression
that may appear on the right-hand side. The set of r-values is a subset
of the the set of l-values. For example, given:
int a;
int b[50];
The following are l-values (and also r-values):
a
b[10]
Whereas the following are ONLY r-values:
17
a + 5
b
Therefore, the following line of code will fail to compile:
a + 5 = 15;
Hope that helps,
Where are the rules of what may go on each side?
A nice rule is "when it makes sense". An l-value is something you can
(after evaluation) assign a value to and an r-value is a value (again,
after evaluation) that can be assigned. Generally speaking this means
that an l-value evaluates to a variable (or and index into an array)
while an r-value evaluates to a value of the same type as the l-value.
Erik Wikström
L-value -> something u can store to -> mem location
R-value -> something u can read from -> variable
Erik said:I'd say R-value -> something with a value -> variable, constant, etc.
Zach said:Can someone please explain what this means and illustrate the
difference with some code.
Thanks,
Zach
lvalues, provided they do not have the type "array of ..." :From Harbison & Steele, 5th ed., the following expressions may be
Erik Wikström said:I'd say R-value -> something with a value -> variable, constant, etc.
John Bode said:lvalues, provided they do not have the type "array of ..." :From Harbison & Steele, 5th ed., the following expressions may be
e // e must be a variable name
e[k]
(e) // e must be an lvalue
e.name // e must be an lvalue
e->name
*e
string-constant
The following expressions may *not* be lvalues:
array names
functions
enumeration constants
assignment expressions
casts
function calls
Chris said:John Bode said:lvalues, provided they do not have the type "array of ..." :From Harbison & Steele, 5th ed., the following expressions may be
e // e must be a variable name
e[k]
(e) // e must be an lvalue
e.name // e must be an lvalue
e->name
*e
string-constant
The following expressions may *not* be lvalues:
array names
functions
enumeration constants
assignment expressions
casts
function calls
Array names? Isn't that the same mistake I made a bit earlier?
John Bode said:An lvalue is an expression that refers to an object (a region of
memory) in such a way that the object may be read or modified. An
rvalue is an expression that is not an lvalue.
Chris said:John Bode said:lvalues, provided they do not have the type "array of ..." :From Harbison & Steele, 5th ed., the following expressions may be
e // e must be a variable name
e[k]
(e) // e must be an lvalue
e.name // e must be an lvalue
e->name
*e
string-constant
The following expressions may *not* be lvalues:
array names
functions
enumeration constants
assignment expressions
casts
function calls
Array names? Isn't that the same mistake I made a bit earlier?
muttaa said:Mr.Chris, will you please explain how array names can be an l-value?
Since there is a lot of confusion in that discussion, I'll give rulesZach said:Chris said:Sure. An l-value is an expression that may legally appear on the left-
hand side of the assignment operator, =. An r-value is an expression
that may appear on the right-hand side. The set of r-values is a subset
of the the set of l-values. For example, given:
int a;
int b[50];
The following are l-values (and also r-values):
a
b[10]
Whereas the following are ONLY r-values:
17
a + 5
b
Therefore, the following line of code will fail to compile:
a + 5 = 15;
Hope that helps,
Where are the rules of what may go on each side?
"From the C++ standard : an lvalue-to-rvalue conversion is:
" 4.2 Array-to-pointer conversionFrom the C++ standard:
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.