l-values and r-values
Hi everyone,
I think I mostly understand l-values and r-values after reading a few
archived posts on this board:
* l-values usually appear on the LHS of an assignment, r-values
usually appear on the RHS of an assignment
An r-value might appear on the LHS of an assignment because of the
following:
* C++ allows a member function to be invoked on a temporary object
* A function returning by value is an r-value
* The assignment operator can be a member function of a UDT
The combination of these factors means that an r-value can appear on
the LHS of an assignment operator:
Foo GetTemp() {return Foo;}
GetTemp() = 5; // which is equivalent to
GetTemp.operator=(5); //
(this of course assumes that the appropriate conversion constructors
and assignment operators are provided for class Foo)
However, I still don't understand how a string literal can be
considered an l-value. The following excerpts hint at an answer:
"String literal: because early C did not have 'const', and so old C
functions that take 'char*' as argument could not be called with
literal strings as actual arguments if string literals were considered
rvalues. However, that may still change. It's just an old
compatibility feature on its way out" - http://tiny.cc/yEr2A
"char *x and char x[] are often used to refer to strings (null
terminated arrays of characters), and they differ in their "lvalue-
ness". " - http://tiny.cc/9JJBG
Wouldn't the following:
void foo(char *);
foo("Hello");
be equivalent to:
char * blah = "Hello";
foo(blah);
? I don't see how this effects the l-valueness of a string literal
Also, is it a defining characteristic of an r-value that you can not
take the address of an r-value (obviously this is true for most cases,
I was wondering if it is, in fact, a defining characteristic, or
whether there are some exceptions)
Taras