Topic: rvalue - lvalue question


Author: "Gil Shafriri" <gilsh@microsoft.com>
Date: 2000/09/18
Raw View
struct A{};

A f(){return A();}

f() = A();

This one compiles OK under VC6. I know that function that returns object by
value  considered as "rvalue".

Can you assign value to rvalue expression ? If So, I admit I totally lost
all my intuition concerning what lvalues

and rvalues really are. Does anyone have intuitive rule for what lvalue \
rvalue really are ?

Thanks,

Gil



---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Gabriel Dos Reis <gdr@codesourcery.com>
Date: 2000/09/18
Raw View
"Gil Shafriri" <gilsh@microsoft.com> writes:

| struct A{};
|
| A f(){ return A(); };
|
| f() = A();
|
| This one compiles OK under VC6. I know that function that returns object by
| value is considered as "rvalue".
|
| Can you assign value to rvalue expression ? If So, I admit I totally lost
| all my intuition concerning what lvalues

The important thing to keep to have at the foreground is that it is
permitted to invoke non-const member function on rvalues.

Next, the copy and assignment operator is just a member function. So

 f() = A();

is semantically equivalent to

 f().operator=(A());

wherer A::operator= is synthetized by the compiler.

|
| and rvalues really are. Does anyone have intuitive rule for what lvalue \
| rvalue really are ?

Intuitive is the keyword ;-)

   lvalue = an expression that _locates_ an object

   rvalue = object representation of a value

alternatively

   lvalue = locate value

   rvalue = read value

If you think the explanation above is obscure, then please remember
that the Standard text is not clearer <g>.

--
Gabriel Dos Reis, gdr@codesourcery.com
CodeSourcery, LLC  http://www.codesourcery.com
       http://www.codesourcery.com/gcc-compile.shtml

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 2000/09/18
Raw View
Gil Shafriri wrote:
>
> struct A{};
>
> A f(){ return A(); };
>
> f() = A();
>
> This one compiles OK under VC6. I know that function that returns object by
> value is considered as "rvalue".
>
> Can you assign value to rvalue expression ? If So, I admit I totally lost
> all my intuition concerning what lvalues
>
> and rvalues really are. Does anyone have intuitive rule for what lvalue \
> rvalue really are ?

The point is, that for classes, you can call member functions on
rvalues, including non-const member functions, if the rvalue is
not const.
Now, the copy assignment operator _is_ such a member (be it
user-declared or compiler-generated). Therefore for classes, the
assignment operator can be called on rvalues.

That's quite ironic, given that the names "lvalue" and "rvalue"
originally are derived from the places they can be in assignment
expressions ;-)

BTW, all rvalues I know are connected with temporaries (for
numeric literals, that temporary can be viewed as optimized away).
Therefore I think the terms "rvalue" and "temporary" can be
equated. Do I miss something here?

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Gil Shafriri" <gilsh@microsoft.com>
Date: 2000/09/18
Raw View
struct A{};

A f(){return A();}

f() = A();

This one compiles OK under VC6. I know that function that returns object by
value is considered as "rvalue".

Can you assign value to rvalue expression ? If So, I admit I totally lost
all my intuition concerning what lvalues

and rvalues really are. Does anyone have intuitive rule for what lvalue \
rvalue really are ?

Thanks,

Gil



---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: 2000/09/18
Raw View
In article <39c4ae55@news.microsoft.com>, Gil Shafriri
<gilsh@microsoft.com> writes
>struct A{};
>
>A f(){return A();}
>
>f() = A();
>
>This one compiles OK under VC6. I know that function that returns object
>by value  considered as "rvalue".
>
>Can you assign value to rvalue expression ? If So, I admit I totally
>lost all my intuition concerning what lvalues
>
>and rvalues really are. Does anyone have intuitive rule for what lvalue
>\ rvalue really are ?

We broke the rvalue concept when we allowed non-const member functions
to be called by a temporary of a udt type.  Unfortunately, assignment is
a member function.



Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Gabriel Dos Reis <gdr@codesourcery.com>
Date: 2000/09/18
Raw View
Francis Glassborow <francis.glassborow@ntlworld.com> writes:

| In article <39c4ae55@news.microsoft.com>, Gil Shafriri
| <gilsh@microsoft.com> writes
| >struct A{};
| >
| >A f(){return A();}
| >
| >f() = A();
| >
| >This one compiles OK under VC6. I know that function that returns object
| >by value  considered as "rvalue".
| >
| >Can you assign value to rvalue expression ? If So, I admit I totally
| >lost all my intuition concerning what lvalues
| >
| >and rvalues really are. Does anyone have intuitive rule for what lvalue
| >\ rvalue really are ?
|
| We broke the rvalue concept when we allowed non-const member functions
| to be called by a temporary of a udt type.

OK.  But then what is (was?) the "rvalue concept"?

(Yes, this is a rhetorical question -- the Standard text isn't that
crystal clear about what it means by lvalue ans rvalue)

--
Gabriel Dos Reis, gdr@codesourcery.com
CodeSourcery, LLC  http://www.codesourcery.com
       http://www.codesourcery.com/gcc-compile.shtml

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Gil Shafriri" <gilsh@microsoft.com>
Date: 2000/09/19
Raw View
 From the standard:

3.10 Lvalues & rvalues

(1) Every expression is either an lvalue or an rvalue.

(5) The result of calling a function that does not return a reference is an
rvalue....

(10) An lvalue for an object is necessary in order to modify the object
except that an rvalue of class type can also be used to modify its referent
under certain circumstances. [Example: a member function called for an
object (9.3) can modify the object.]

5.17 Assignment operators

(1) There are several assignment operators [...]. All require a modifiable
lvalue as their left operand....



Could someone explain  to me how 3.10\10 can exists with 5.17\1 ?  5.17\1
makes  it clear that you can't assigne to function return value, on the
other hand

3.10\10 say you can modify rvalue by calling memeber function. Any idea what
is going on here ?



Thanks,

Gil









"Gabriel Dos Reis" <gdr@codesourcery.com> wrote in message
news:m3n1h526ps.fsf@merlin.codesourcery.com...
> "Gil Shafriri" <gilsh@microsoft.com> writes:
>
> | struct A{};
> |
> | A f(){ return A(); };
> |
> | f() = A();
> |
> | This one compiles OK under VC6. I know that function that returns object
by
> | value is considered as "rvalue".
> |
> | Can you assign value to rvalue expression ? If So, I admit I totally
lost
> | all my intuition concerning what lvalues
>
> The important thing to keep to have at the foreground is that it is
> permitted to invoke non-const member function on rvalues.
>
> Next, the copy and assignment operator is just a member function. So
>
> f() = A();
>
> is semantically equivalent to
>
> f().operator=(A());
>
> wherer A::operator= is synthetized by the compiler.
>
> |
> | and rvalues really are. Does anyone have intuitive rule for what lvalue
\
> | rvalue really are ?
>
> Intuitive is the keyword ;-)
>
>    lvalue = an expression that _locates_ an object
>
>    rvalue = object representation of a value
>
> alternatively
>
>    lvalue = locate value
>
>    rvalue = read value
>
> If you think the explanation above is obscure, then please remember
> that the Standard text is not clearer <g>.
>
> --
> Gabriel Dos Reis, gdr@codesourcery.com
> CodeSourcery, LLC http://www.codesourcery.com
>        http://www.codesourcery.com/gcc-compile.shtml
>
> ---
> [ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]
>


---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Gabriel Dos Reis <gdr@codesourcery.com>
Date: 2000/09/19
Raw View
"Gil Shafriri" <gilsh@microsoft.com> writes:

[...]

| Could someone explain  to me how 3.10\10 can exists with 5.17\1 ?  5.17\1
| makes  it clear that you can't assigne to function return value, on the
| other hand

5/2
  Operators can be overloaded, that is, given meaning when applied to
  expressions of class type (clause 9) or enumeration type (7.2).
  Uses of overloaded operators are transformed into function calls as
  described in 13.5.  Overloaded operators obey the rules for syntax
  specified in clause 5, but the requirement of operand type, lvalue,
  and evaluation order are replaced by the rules for function call. [...]

5/3
  Clause 5 defines the effects of operators when applied for types for
  which they have not been overloaded. [...]

--
Gabriel Dos Reis, gdr@codesourcery.com
CodeSourcery, LLC  http://www.codesourcery.com
       http://www.codesourcery.com/gcc-compile.shtml

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: 2000/09/20
Raw View
In article <m3aed4xu4x.fsf@merlin.codesourcery.com>, Gabriel Dos Reis
<gdr@codesourcery.com> writes
>5/2
>  Operators can be overloaded, that is, given meaning when applied to
>  expressions of class type (clause 9) or enumeration type (7.2).
>  Uses of overloaded operators are transformed into function calls as
>  described in 13.5.  Overloaded operators obey the rules for syntax
>  specified in clause 5, but the requirement of operand type, lvalue,
>  and evaluation order are replaced by the rules for function call.
>[...]

And copy assignment is implicitly overloaded for class types, and cannot
be overloaded for enum types.


Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Gil Shafriri" <gilsh@microsoft.com>
Date: 2000/09/18
Raw View
struct A{};

A f(){ return A(); };

f() = A();

This one compiles OK under VC6. I know that function that returns object by
value is considered as "rvalue".

Can you assign value to rvalue expression ? If So, I admit I totally lost
all my intuition concerning what lvalues

and rvalues really are. Does anyone have intuitive rule for what lvalue \
rvalue really are ?

Thanks,

Gil



---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Anders Pytte <anders@milkweed.com>
Date: 2000/09/18
Raw View
in article 39c50c0a@news.microsoft.com, Gil Shafriri at gilsh@microsoft.com
wrote on 9/18/00 8:51 AM:

> struct A{};
>
> A f(){ return A(); };
>
> f() = A();
>
> This one compiles OK under VC6. I know that function that returns object by
> value is considered as "rvalue".
>
> Can you assign value to rvalue expression ? If So, I admit I totally lost
> all my intuition concerning what lvalues
>
> and rvalues really are. Does anyone have intuitive rule for what lvalue \
> rvalue really are ?

An lvalue is an expression that refers to "something in memory". My favorite
discussion of this is Stroustrup C++PL 3rd. 4.9.6. Not all lvalues are
modifiable, however.

I believe that theC++ implementation is allowed to create a temporary lvalue
from the function result, and that non-const operations are allowed on that
temporary within the full expression.

Anders.



--
Anders Pytte                                   Milkweed Software
PO Box 32                                  voice: (802) 586-2545
Craftsbury, VT 05826                  email: anders@milkweed.com

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]