Topic: the meaning of lvalue in C++
Author: dave.thompson2@verizon.net (David Thompson)
Date: Sun, 15 Apr 2007 14:40:54 GMT Raw View
On Mon, 12 Mar 2007 12:00:58 CST, "James Kanze"
<james.kanze@gmail.com> wrote:
> 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.
>
In BCPL, the primary ancestor of C, which had/modelled only uniformly
writable storage, it was simply any storage address.
> Except that that use was quickly violated in C as well, since
> you can have const lvalues.
>
Not so quick, about 15 years. But arrays were unassignable lvalues
from the beginning of C -- as opposed to BCPL and IIRC B where they
were truly just pointers, as many people still wrongly believe and say
for C and (perhaps less so) C++.
---
[ 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 ]
Author: "jam" <farid.mehrabi@gmail.com>
Date: Sat, 17 Mar 2007 10:00:14 CST Raw View
On Mar 12, 10:00 pm, "James Kanze" <james.ka...@gmail.com> wrote:
> 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.ka...@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-...@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ:http://www.comeaucomputing.com/csc/faq.html ]
Except literals nothing should be constreined to be none
changing.Liberity is a divine gift.I do not see any thing wrong with
this in C++.
---
[ 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 ]
Author: "restor" <akrzemi1@interia.pl>
Date: Wed, 14 Mar 2007 15:45:40 CST Raw View
So, would it be correct to say, that rvalue is either a literal (or
something literal-like: enum, initializer-list) or a temporary?
&rzej
---
[ 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 ]
Author: "James Kanze" <james.kanze@gmail.com>
Date: Thu, 15 Mar 2007 09:49:52 CST Raw View
On Mar 14, 10:45 pm, "restor" <akrze...@interia.pl> wrote:
> So, would it be correct to say, that rvalue is either a literal (or
> something literal-like: enum, initializer-list) or a temporary?
More or less, maybe. My own position is simply that rvalue and
lvalue are arbitrary categories, with no real meaning outside of
the standard's specification of expressions, where it is
explicitly stated whether the results of each expression are an
rvalue or an lvalue, and whether the expression requires an
rvalue or an lvalue. (There is an lvalue to rvalue conversion,
so an lvalue can always be used where an rvalue is required.)
In the end, today, it can be argued that the distinction is
nothing more than a way of artificially constraining
expressions. But such constraints have a historical origin, and
participate in the "look and feel" of the language; allowing
things like &3 or &(i+2) just doesn't "feel" right. Even though
there are situations where such expressions do have an address,
and it is possible to obtain it.
--
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 ]
Author: "restor" <akrzemi1@interia.pl>
Date: Fri, 9 Mar 2007 14:25:43 CST Raw View
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. But
in C++ the code:
std::complex<double> Complex() { return std::complex<double>(); }
Complex() = Complex();
Is valid. 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???
ref = 3;
Is valid, but it is unclear (at least for me) what is 'ref'. Is it an
lvalue pointing to a temporary?
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++?
---
[ 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 ]
Author: "jam" <farid.mehrabi@gmail.com>
Date: Fri, 9 Mar 2007 17:26:48 CST Raw View
On Mar 10, 12:25 am, "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. But
> in C++ the code:
>
> std::complex<double> Complex() { return std::complex<double>(); }
> Complex() = Complex();
>
> Is valid. 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???
> ref = 3;
>
> Is valid, but it is unclear (at least for me) what is 'ref'. Is it an
> lvalue pointing to a temporary?
> 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++?
>
What is the use of an lvalue by the way?
I should say that even const objects can be lvalue:
struct Lvalue{
Lvalue operator=(const Lvalue &)const{};//look at this!!
};
{
const Lvalue L1,L2;
L1=L2;//huh huh look at me
};
generally the only real Rvalue in C++ are literal numbers and
enumerations.
Have fun
FM
---
[ 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 ]
Author: e0226430@stud3.tuwien.ac.at (Sebastian Redl)
Date: Mon, 12 Mar 2007 17:00:32 GMT Raw View
On Fri, 9 Mar 2007, restor 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.
I believe even in C const variables are lvalues. But I'm not sure about
that.
Anyway, an lvalue can have its address taken. This is not possible with an
rvalue.
Sebastian Redl
---
[ 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 ]
Author: "James Kanze" <james.kanze@gmail.com>
Date: Mon, 12 Mar 2007 12:00:58 CST Raw View
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 ]
Author: tasjaevan@gmail.com
Date: Mon, 12 Mar 2007 13:19:11 CST Raw View
On Mar 9, 11:26 pm, "jam" <farid.mehr...@gmail.com> wrote:
> On Mar 10, 12:25 am, "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. But
> > in C++ the code:
>
> > std::complex<double> Complex() { return std::complex<double>(); }
> > Complex() = Complex();
>
> > Is valid. 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???
> > ref = 3;
>
> > Is valid, but it is unclear (at least for me) what is 'ref'. Is it an
> > lvalue pointing to a temporary?
> > 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++?
>
> What is the use of an lvalue by the way?
> I should say that even const objects can be lvalue:
>
> struct Lvalue{
> Lvalue operator=(const Lvalue &)const{};//look at this!!
>
> };
>
> {
> const Lvalue L1,L2;
> L1=L2;//huh huh look at me
>
> };
>
> generally the only real Rvalue in C++ are literal numbers and
> enumerations.
>
Also:
- non-reference return types (including some built-in operations,
such as ! and unary &)
- explicitly constructed temporaries (e.g. T(3), where T is a type
constructible from an int)
- the 'this' pointer
James
---
[ 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 ]
Author: "James Kanze" <james.kanze@gmail.com>
Date: Tue, 13 Mar 2007 08:47:36 CST Raw View
On Mar 10, 12:26 am, "jam" <farid.mehr...@gmail.com> wrote:
> On Mar 10, 12:25 am, "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. But
> > in C++ the code:
> > std::complex<double> Complex() { return std::complex<double>(); }
> > Complex() = Complex();
> > Is valid. 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???
> > ref = 3;
> > Is valid, but it is unclear (at least for me) what is 'ref'. Is it an
> > lvalue pointing to a temporary?
> > 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++?
> What is the use of an lvalue by the way?
> I should say that even const objects can be lvalue:
Even in C.
> struct Lvalue{
> Lvalue operator=(const Lvalue &)const{};//look at this!!
>
> };
> {
> const Lvalue L1,L2;
> L1=L2;//huh huh look at me
>
> };
> generally the only real Rvalue in C++ are literal numbers and
> enumerations.
And most expressions. What's particular about class type
temporaries isn't that they aren't rvalues; it's that it's
possible, indirectly, to get their address (via the this
pointer) even though they are rvalues.
As for the "utility": I think it's largely a question of C
compatibility. Not just at the language level, but at a larger
"look and feel" level. One could easily do away with the
concept entirely, allowing things like "&(i + 3)" (with type
const int? and of course, the usual lifetime of a temporary),
but somehow, the language wouldn't feel the same.
--
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 ]