Original Post
🔒 Locked
lvalues & rvalues
Advertisement
I think that lvalue means "read-write" and rvalue means "write-only." It is basically a compiler convention that prevents you from doing stupid stuff, such as:
Since someclass::block() is a function that returns its data member by value, the returned object is merely a temporary; setting it to true is meaningless because the temporary goes out of scope immediately after the assignment. The compiler will raise hell about this because it will prevent you from making stupid mistakes.
Gotta go now, or I''d polish this up a bit. Maybe someone else can elaborate on this.
- null_pointer
class someclass
{
bool block_;
public:
bool block() { return block_; }
};
int main()
{
someclass someinstance;
someinstance.block() = true; // error: not an lvalue
}
Since someclass::block() is a function that returns its data member by value, the returned object is merely a temporary; setting it to true is meaningless because the temporary goes out of scope immediately after the assignment. The compiler will raise hell about this because it will prevent you from making stupid mistakes.
Gotta go now, or I''d polish this up a bit. Maybe someone else can elaborate on this.
- null_pointer
From MSDN: (why don't people ever use it)
L-Values and R-Values
Expressions in C++ can evaluate to “l-values” or “r-values.” L-values are expressions that evaluate to a type other than void and that designate a variable.
L-values appear on the left side of an assignment statement (hence the “l” in l-value). Variables that would normally be l-values can be made nonmodifiable by using the const keyword; these cannot appear on the left of an assignment statement. Reference types are always l-values.
The term r-value is sometimes used to describe the value of an expression and to distinguish it from an l-value. All l-values are r-values but not all r-values are l-values.
Some examples of correct and incorrect usages are:
Edited by - siaspete on December 16, 2001 10:40:28 AM
L-Values and R-Values
Expressions in C++ can evaluate to “l-values” or “r-values.” L-values are expressions that evaluate to a type other than void and that designate a variable.
L-values appear on the left side of an assignment statement (hence the “l” in l-value). Variables that would normally be l-values can be made nonmodifiable by using the const keyword; these cannot appear on the left of an assignment statement. Reference types are always l-values.
The term r-value is sometimes used to describe the value of an expression and to distinguish it from an l-value. All l-values are r-values but not all r-values are l-values.
Some examples of correct and incorrect usages are:
|
Edited by - siaspete on December 16, 2001 10:40:28 AM
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement