Re: the meaning of lvalue in C++
On Mar 9, 9:25 pm, "restor" <akrze...@interia.pl> wrote:
Does lvalue have any useful meaning in C++? The original (i.e.: as
used in C) was that it is whatever that can be assigned a value.
Except that that use was quickly violated in C as well, since
you can have const lvalues.
But in C++ the code:
std::complex<double> Complex() { return std::complex<double>(); }
Complex() = Complex();
Is valid.
The value of lvalues is somewhat (but not completely)
attenuated for class types.
Also the code:
int Integer() { return 1; }
const int& cref = Integer();
int& ref = const_cast<int&>( cref ); // is it a cast from rvalue to
lvalue???
No. cref is an lvalue to begin with.
ref = 3;
Is valid,
No it's not. It's undefined behavior.
You can get similar situations in C:
int const i = 3 ;
int const* p = &i ;
int* q = (int*) p ;
*1 = 4 ;
Also undefined behavior (both in C and in C++).
but it is unclear (at least for me) what is 'ref'. Is it an
lvalue pointing to a temporary?
It's a non-const reference to an int. It's also an lvalue. And
yes, it happens to designate a temporary; that's something that
I don't think you can do in C. On the other hand, as long as
the temporary is alife, why not? The real problem is that you
can get lvalue expresions (both in C and in C++) which refer to
objects which no longer exist. Which results in undefined
behavior in both languages.
We also have non-modifiable lvalues (consts) and modifiable rvalues
like here:
ReturnObj().ModifyMe();
So what is useful in having a concept of lvalue in C++?
Mostly, I think, for the non-class types. It's mainly relevant
in exprssions, and of course, about the only thing you can do
with a non-class types in an expression is call functions (on it
or with it as an argument). But it still places a small role
with class types: &Class() isn't legal, for example, and you
cannot bind an rvalue directly to a non-const reference.
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]