C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

the meaning of lvalue in C++

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
restor
Guest





PostPosted: Fri Mar 09, 2007 8:25 pm    Post subject: the meaning of lvalue in C++ Reply with quote



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 ]
Back to top
jam
Guest





PostPosted: Fri Mar 09, 2007 11:26 pm    Post subject: Re: the meaning of lvalue in C++ Reply with quote



On Mar 10, 12:25 am, "restor" <akrze...@interia.pl> wrote:
Quote:
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 ]
Back to top
James Kanze
Guest





PostPosted: Mon Mar 12, 2007 6:00 pm    Post subject: Re: the meaning of lvalue in C++ Reply with quote



On Mar 9, 9:25 pm, "restor" <akrze...@interia.pl> wrote:
Quote:
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.

Quote:
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.

Quote:
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.

Quote:
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++).

Quote:
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.

Quote:
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 (AT) gmail (DOT) 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 ]
Back to top
Guest






PostPosted: Mon Mar 12, 2007 7:19 pm    Post subject: Re: the meaning of lvalue in C++ Reply with quote

On Mar 9, 11:26 pm, "jam" <farid.mehr...@gmail.com> wrote:
Quote:
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 ]
Back to top
James Kanze
Guest





PostPosted: Tue Mar 13, 2007 2:47 pm    Post subject: Re: the meaning of lvalue in C++ Reply with quote

On Mar 10, 12:26 am, "jam" <farid.mehr...@gmail.com> wrote:
Quote:
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.

Quote:
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 (AT) gmail (DOT) 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 ]
Back to top
restor
Guest





PostPosted: Wed Mar 14, 2007 9:45 pm    Post subject: Re: the meaning of lvalue in C++ Reply with quote

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 ]
Back to top
James Kanze
Guest





PostPosted: Thu Mar 15, 2007 3:49 pm    Post subject: Re: the meaning of lvalue in C++ Reply with quote

On Mar 14, 10:45 pm, "restor" <akrze...@interia.pl> wrote:
Quote:
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 (AT) gmail (DOT) 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 ]
Back to top
jam
Guest





PostPosted: Sat Mar 17, 2007 4:00 pm    Post subject: Re: the meaning of lvalue in C++ Reply with quote

On Mar 12, 10:00 pm, "James Kanze" <james.ka...@gmail.com> wrote:
Quote:
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 ]
Back to top
David Thompson
Guest





PostPosted: Sun Apr 15, 2007 7:40 pm    Post subject: Re: the meaning of lvalue in C++ Reply with quote

On Mon, 12 Mar 2007 12:00:58 CST, "James Kanze"
<james.kanze (AT) gmail (DOT) com> wrote:

Quote:
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.

Quote:
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 ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.