rvalue or lvalue?

Hi all,

Consider the following example:

struct Test {
    const Test& operator=( const Test &) const{
        return *this;
    }
};

int main(int argc, char* argv[])
{
    Test() = Test();
	return 0;
}

This adds a funny twist to the meaning lvalue, because in this case
every rvalue is an lvalue as well ;)

Best,
John

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
jtorjo
11/9/2003 2:21:28 PM
comp.lang.c++.moderated 10706 articles. 11 followers. allnor (8507) is leader. Post Follow

11 Replies
240 Views

Similar Articles

[PageSpeed] 19
> struct Test {
>     const Test& operator=( const Test &) const{
>         return *this;
>     }
> };

> int main(int argc, char* argv[])
> {
>     Test() = Test();
> return 0;
> }
>
> This adds a funny twist to the meaning lvalue, because in this case
> every rvalue is an lvalue as well ;)

Not really--it's just that rvalues of this type support assignment.

You shouldn't be able to execute &Test(), for example.


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Andrew
11/9/2003 10:36:57 PM
John Torjo wrote:

> Consider the following example:
> 
> struct Test {
>     const Test& operator=( const Test &) const{
>         return *this;
>     }
> };
> 
> int main(int argc, char* argv[])
> {
>     Test() = Test();
> return 0;
> }
> 
> This adds a funny twist to the meaning lvalue, because in this case
> every rvalue is an lvalue as well ;)

If you look at the title, the document pointed to is ancient, mostly
C-related, but still instructive...

        http://members.home.nl/r.f.pels/articles/misc/rlvalue.txt

-- 
Ruurd
..o.
...o
ooo

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
R
11/9/2003 10:49:59 PM
On 9 Nov 2003 09:21:28 -0500, jtorjo@yahoo.com (John Torjo) wrote:

> Consider the following example:

> struct Test {
>     const Test& operator=( const Test &) const{
>         return *this;
>     }
> };

> int main(int argc, char* argv[])
> {
>     Test() = Test();
> 	return 0;
> }

> This adds a funny twist to the meaning lvalue, because in this case
> every rvalue is an lvalue as well ;)

Your non-standard operator= reduced the possibilities.  If you stick to
the standard way, you can do more.

   (Test() = Test()) = Test();

What's the problem?  There is a standard lvalue to rvalue implicit
conversion which is used whenever needed.  There is a standard rvalue
to non-modifiable lvalue implicit conversion which is used whenever
needed.  There is a standard rvalue to modifiable lvalue explicit
conversion which may use whenever you like with user defined types.
The poor fundamental types are broken.

   int() = int();

Lvalue required.  We need to use the more complex version to work with
fundamental types.

   const_cast<int&>(static_cast<int const&>(int())) = 42;

Be sure to wear your bullet proof shoes.

John

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
John
11/9/2003 10:52:44 PM
> 
> Lvalue required.  We need to use the more complex version to work with
> fundamental types.
> 
>    const_cast<int&>(static_cast<int const&>(int())) = 42;
> 
Yup ;)

Note: I only said it adds a funny twist, I didn't say I have anything
against it. The example I've shown is perfectly ok, and works as
expected.

The reason I've posted is is because usually people (including me;) )
think of rvalue as "what can only be on the right side of operator="
(while, as I've shown, it's not true).

Best,
John

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
jtorjo
11/10/2003 10:53:23 AM
John Torjo wrote:
 > Consider the following example:
 >
 > struct Test {
 >     const Test& operator=( const Test &) const{
 >         return *this;
 >     }
 > };
 >
 > int main(int argc, char* argv[])
 > {
 >     Test() = Test();
 >       return 0;
 > }
 >
 > This adds a funny twist to the meaning lvalue, because in this case
 > every rvalue is an lvalue as well ;)
 > ...

I don't see what makes you to come to that conclusion.

The fact that you can place a temporary object on the left-hand side of
the assignment operator? But that's not how the term 'lvalue' is defined.

The fact that the standard requires lvalue on the left-hand side of
built-in assignment operators? But that only applies to _built-in_
assignment operators. There's no such requirement for user-defined
assignment operator.

-- 
Best regards,
Andrey Tarasevich


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Andrey
11/10/2003 1:28:09 PM
John Torjo <jtorjo@yahoo.com> schrieb in im Newsbeitrag:
c638aac5.0311082333.375479aa@posting.google.com...
> Hi all,
>
> Consider the following example:
>
> struct Test {
>     const Test& operator=( const Test &) const{
>         return *this;
>     }
> };
>
> int main(int argc, char* argv[])
> {
>     Test() = Test();
> return 0;
> }
>
> This adds a funny twist to the meaning lvalue, because in this case
> every rvalue is an lvalue as well ;)
>

Maybe you are mixing up operator semantics with function call semantics. You
example basically calls

Test().operator=( Test() );

which is actually not a 'real' assignment but a function call. It's possible
to call a function on an rvalue.

Best regards,

Matthias




      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Matthias
11/10/2003 7:48:18 PM
John Potter <jpotter@falcon.lhup.edu> schrieb in im Newsbeitrag:
qktsqvga4cge0d3koj8951f72597m6dem1@4ax.com...
> On 9 Nov 2003 09:21:28 -0500, jtorjo@yahoo.com (John Torjo) wrote:
>
> [snipping rxample]
>
> What's the problem?  There is a standard lvalue to rvalue implicit
> conversion which is used whenever needed.  There is a standard rvalue
> to non-modifiable lvalue implicit conversion which is used whenever
> needed.  There is a standard rvalue to modifiable lvalue explicit
> conversion which may use whenever you like with user defined types.
> The poor fundamental types are broken.
>
>    int() = int();
>
> Lvalue required.  We need to use the more complex version to work with
> fundamental types.
>
>    const_cast<int&>(static_cast<int const&>(int())) = 42;
>
> Be sure to wear your bullet proof shoes.
>

These rvalue to lvalue conversions have always confused me - could you give
a brief explanation and what it has to do with the "Test() = Test()"
example?

Best regards,

Matthias Hofmann




      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Matthias
11/10/2003 7:56:18 PM
R.F. Pels wrote:
<snip> 
> If you look at the title, the document pointed to is ancient, mostly
> C-related, but still instructive...
> 
>         http://members.home.nl/r.f.pels/articles/misc/rlvalue.txt

You do not appear to have understood the terminology yourself when you
wrote it.  Rvalues and lvalues are kinds of expression, not attributes
of variables.  Furthermore, you seem to have their meanings the wrong
way round.  An rvalue yields a value whereas an lvalue yields a
reference to (at the machine level, an address of) an object or
function.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Ben
11/11/2003 10:08:43 AM
On 10 Nov 2003 14:56:18 -0500, "Matthias Hofmann"
<hofmann@anvil-soft.com> wrote:

 > John Potter <jpotter@falcon.lhup.edu> schrieb in im Newsbeitrag:
 > qktsqvga4cge0d3koj8951f72597m6dem1@4ax.com...

 > > What's the problem?  There is a standard lvalue to rvalue implicit
 > > conversion which is used whenever needed.  There is a standard rvalue
 > > to non-modifiable lvalue implicit conversion which is used whenever
 > > needed.  There is a standard rvalue to modifiable lvalue explicit
 > > conversion which may use whenever you like with user defined types.
 > > The poor fundamental types are broken.

 > >    int() = int();

 > > Lvalue required.  We need to use the more complex version to work with
 > > fundamental types.

 > >    const_cast<int&>(static_cast<int const&>(int())) = 42;

 > > Be sure to wear your bullet proof shoes.

 > These rvalue to lvalue conversions have always confused me - could you give
 > a brief explanation and what it has to do with the "Test() = Test()"
 > example?

int const& r(42);

In this, 42 is an rvalue and r is an unmodifiable lvalue.  It is allowed
to convert an rvalue to an unmodifiable lvalue by binding an rvalue to
a reference to const.  All references are lvalues.  Not all lvalues are
references say the language lawyers.

Since the above initialization of r is allowed, the static_cast of the
rvalue int() (a cute typed name for 0) is also allowed.  Now that we
have an lvalue, we can remove const with an lvalue const_cast and assign
to the thing which is about to self destruct.

In Test() = Test(), the left hand side is an rvalue, but operator= is a
member function which may be called on rvalues.  We still self destruct
both things.

An example of where these silly things are really useful is a class with
non-member operations which have side effects.

    ofstream("log.file", ios::app) << 42;

Works fine because <<(int) is a member function.

    ofstream("log.file", ios::app) << "Does not work";

Is an error because <<(ostream&, char*) is a non-member and an
rvalue may not be bound to a non-const reference.

    ofstream("log.file", ios::app).flush() << "Works fine";

This works because flush is a member function which may be called on an
rvalue and returns an lvalue which can be passed to the operator<<.

Rvalue to lvalue conversion are mostly amusing results of non-const
members being callable on rvalues.  In the above example, operator=
for streams is private and flush is public.  In both cases, the member
returns a reference which is an lvalue.

Now seeing your other post, this may also be useful.  In the case of
Test() = Test(), the simple call a function on an rvalue was used as
you said.  The non-standard return of operator= as Test const& still
converts an rvalue to an lvalue.

   (Test() = Test()) = Test();

This is invalid because it requires a modifiable lvalue and the
Test::operator= returned a non-modifiable lvalue.  We can "fix"
this as we did with int but in only one step since we have an
lvalue.

    const_cast<Test&>(Test() = Test()) = Test();

John

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
John
11/11/2003 4:50:43 PM
John Potter <jpotter@falcon.lhup.edu> schrieb in im Newsbeitrag:
06c0rvockqp7n1kvhld9h5a733fmtf0j02@4ax.com...
> On 10 Nov 2003 14:56:18 -0500, "Matthias Hofmann"
> <hofmann@anvil-soft.com> wrote:
>

[snip]

>
> Now seeing your other post, this may also be useful.  In the case of
> Test() = Test(), the simple call a function on an rvalue was used as
> you said.  The non-standard return of operator= as Test const& still
> converts an rvalue to an lvalue.
>
>    (Test() = Test()) = Test();
>
> This is invalid because it requires a modifiable lvalue and the
> Test::operator= returned a non-modifiable lvalue.  We can "fix"
> this as we did with int but in only one step since we have an
> lvalue.
>
>     const_cast<Test&>(Test() = Test()) = Test();
>

Thank you for the detailed explanation! One thing that puzzles me now is
your last example:

(Test() = Test()) = Test();

As you said, Test::operator=() returns a const reference - however, it also
declares operator=() to be a constant member function. Therefore, I wonder
if it is possible to call a constant member function on a non-modifiable
lvalue, so the above assignment should not be an error? This is the
definition of Test from the original post:

struct Test {
    const Test& operator=( const Test &) const{
        return *this;
    }
};

Best regards,

Matthias




      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Matthias
11/12/2003 8:30:05 PM
On 12 Nov 2003 15:30:05 -0500, "Matthias Hofmann"
<hofmann@anvil-soft.com> wrote:

> (Test() = Test()) = Test();

> As you said, Test::operator=() returns a const reference - however, it also
> declares operator=() to be a constant member function. Therefore, I wonder
> if it is possible to call a constant member function on a non-modifiable
> lvalue, so the above assignment should not be an error? This is the
> definition of Test from the original post:

> struct Test {
>     const Test& operator=( const Test &) const{
>         return *this;
>     }
> };

Right.  I missed the fact than all Test objects are const because they
have no state.  Maybe not the best example to talk about these things.
However, many function objects are of this form.  We usually do not
bother to define the operator= and take the compiler default.

John

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
John
11/14/2003 10:34:04 AM
Reply:
Similar Artilces:

rvalue references and default constructors
Hello, C++0x introduces the concept of rvalue-references (described for example here: http://www.artima.com/cppsource/rvalue.html) and, if I got this right, an rvalue-reference is an invitation to mutilate the referenced object since it is a temporary which will not be used anymore. Which enables us to implement (amongst other things) move semantics. But, inside a function which gets an rvalue-reference it is treated like a normal reference, which makes sense since you don't want your object destroyed after accessing it once. std::move() will turn a normal (lvalue-)reference into an rva...

ERROR: Reference to vector reg 'InKbWd' is not a legal net lvalue
Hello: I am trying to implement a PS2 keyboard read routine. Basically, use the keyboard clock to shift 11 bits through a register. I am getting an odd error. Heres a SMALL snippet that shows the error under ISE 9 ******************************************* module _kb_echo (input ExtClk, input ExtRst, input ExtKbClk, input ExtKbData, output [1:0] ExtLedVcc, output [6:0] ExtLedC); reg [10:0] InKbWd; mod_kb_read _mod_kb_read(ExtRst,ExtKbClk,ExtKbData,I...

Lvalue-to-rvalue conversion vs rvalue reference
Hi, 3.10/7 in N3000 Says: "Whenever an lvalue appears in a context where an rvalue is expected, the lvalue is converted to an rvalue;" On the other hand I was pretty sure that the following code shouldn't compile: void possiblyDestroy( Obj && obj ); int main() { Obj obj; possiblyDestroy( obj ); } In the function call above, an rvalue is expected, "obj" is an lvalue, so it "appears in a context where an rvalue is expected". Is the above code valid? Regards, &rzej -- [ comp.std.c++ is moderated. To subm...

Rvalue Reference question: Overloading on whether *this is an rvalue
I have a question about that rvalue reference proposal in the pre-Redmond mailing (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1690.html). Is there any way to overload a member function on whether *this is an lvalue or an rvalue? Consider the following code: #include <vector> #include <string> vector<string> createVector(); int main() { vector<string> v(1, "Hello World"); std::string s1 = v[0]; std::string s2 = createVector()[0]; return 1; } Since v is an lvalue, s1 has...

qualified rvalues
Hi, I have the impression that the dealing of rvalues with qualified types is largely underspecified. It was so even before C11 but that problem only surfaces now, since with _Generic type qualifications of values have become observable. In essence I didn't find that the standard has much to say when it comes to qualified types for rvalues. They definitively do exist, since the cast operator explicitly specifies that the type could be qualified. The section on casts also has the only indication I found that relates to rvalues. There is a footnote (thus not normative) that says ...

RValue
Hi, I expected this code to give a compilation error but instead it compiles both in VS2005 and Gcc class ABCD{ public: ABCD(int x = 0){} ABCD&operator=(const int x){ return *this; } ABCD(const ABCD& rhs){} ~ABCD(){ } }; ABCD fn(){ return ABCD(); } int main(){ fn() = 2; } As per the standard document "The result of calling a function that does not return a reference is an rvalue. User defined operators are functions, and whether such operators expect or yield lvalues is determined by their parameter and return types." In my understanding, an unnamed temporary ...

rvalue
Hi all, It's been a while! I'm working on a very buggy piece of code that uses a lot of macros. A bunch of them look like #define POSITION(mw,row) (mw.positions[row]) In order to debug the thing I'd like to modify these macors so they can only be used as rvalues and not as lvalues. If they are used as lvalues I want my compililation to fail. Unfortunately I can't remember a nice way to do that. As always thanks for the help! Tobias. Stefan Ram wrote: > Tobias Oed <tobias@physics.odu.edu> writes: >>In order to debug the thing I'd like to modify these mac...

Trap rvalues
[From an example on comp.lang.c] Hi, Consider this simple program: int *foo(void) { int i; return &i; } int *bar(void) { int i; return &i; } int main(void) { return foo() =3D=3D bar(); } Or, maybe even this simpler version: int *foo(void) { int i; return &i; } int main(void) { return foo() =3D=3D foo(); } As far as I can see, the standard specifies that both programs are strictly conforming, and must return 0. This can't possibly be intended. 6=2E2.6.1p5: "Certain object representations need not represent a value of the object type. If the stored...

this by rvalue
I was reading up on the new C++0x features and have a question: Is there a way to overload on the rvalue-ness of the invoking object itself? In other words, distinguish between: // contrived example X x; x.foo(); and: X f(); f().foo(); A less contrived example would be a member X::operator+() which would distinguish between the first and the second invocation in the line below: X x, y, z; X xx = x + y + z So the first + sign would resolve to <lvalue>.operator+(y), and the second + sign would resolve to <rvalue>.operator+(z) (where the second one i...

Static cast to rvalue reference
I'm confused by the syntax below. Why is the syntax for this static_cast not X x1; X&& x2 = static_cast<X&&>(x1); x2 has been cast to the type X&& so why does the syntax make it seem as though x2 is of type X after the cast? Many thanks for your help. Paul CODE THAT CONFUSES ME IS BELOW X x1; X x2 = static_cast<X&&>(x1); On Friday, 12 June 2015 16:13:07 UTC+3, Paul wrote: > I'm confused by the syntax below. Why is the syntax for this static_cast not > X x1; > X&& x2 = static_cast<X&&>(x1...

Lvalue Required(wierd)...
Hi All. What is The Lvalue Required error message. (What does it mean?Is it an abbreviationof something.) I wrote this test program and I am keeping geting this message. void main() { clrscr(); int (*x)[10]; (*x)=(int *) malloc( 30 * sizeof (int) ); for(int i=0;i<5;i++) for(int j=0;j<6;j++) x[i][j]=i*10+j; for(i=0;i<5;i++) for(j=0;j<6;j++) printf("The index is %d%d at address %d having value %d\n",i,j,&x[i][j],x[i][j]); } The compiler is Turbo C for DOS(The same problem with Borland C++ for Dos too). Please e-mail your answers to eng__wes...

RValue Arrays
Hi, $4.2 states "An lvalue or rvalue of type �array of N T� or �array of unknown bound of T� can be converted to an rvalue of type �pointer to T�. The result is a pointer to the first element of the array." I was not sure about what is an rvalue array and hence a little googling threw up the following site "http://209.85.175.104/search? q=cache:r1-djlzAyBUJ:www.open-std.org/jtc1/sc22/wg14/www/docs/n835.ps +An+lvalue+or+rvalue+of+type+%E2%80%9Carray+of+N+T %E2%80%9D&hl=en&ct=clnk&cd=1&gl=in" I would like to know a little more about RValue Array...

Temporary as lvalue
I found out that recent MS VS (maybe elder too) treats temporary to user types as lvalue or allows conversion. So something like this would compile there: //----------------- struct Bar{}; void foo( Bar & ); void f() { foo( Bar() ); } //----------------- Are they moving towards new standard or it is their own initiative? Thanks, Vyacheslav Vyacheslav Kononenko wrote: > I found out that recent MS VS (maybe elder too) treats temporary to > user types as lvalue or allows conversion. So something like this would > compile there: > //----------------- > struct Bar{}; > vo...

invalid lvalue
The following code: int main(void) { char *x; (void **)x += 1; return (0); } gives "invalid lvalue in assignment" when compiled with the 64-bit Fedora Core 4 build of gcc 4.1.1. But it works fine with other versions of gcc, and other compilers, and the operation I want done looks entirely logical to me. Is this code C90-compliant or not? Thanks. Paul. Paul Edwards wrote: > > The following code: > > int main(void) > { > char *x; > > (void **)x += 1; > return (0); > } > > gives "invalid lvalue in assignment&...

Defect Report: lvalue-to-rvalue conversion incorrectly doesn't apply to rvalue arrays
5[expr]/8 contains the following wording Whenever an lvalue expression appears as an operand of an operator that expects an rvalue for that operand, the lvalue-to-rvalue (4.1), array-to- pointer (4.2), or function-to-pointer (4.3) standard conversions are applied to convert the expression to an rvalue. This won't cause the conversion for rvalue arrays. Testcase that i believe is currently rejected by the standards wording (but should be accepted): struct A { int n[1]; }; A f() { return A(); } int main() { f().n[0] = 0; } -- [ comp.std.c++ is moderated. To submit arti...

?: as an lvalue
Hi Everyone, I have the following piece of code, and i expected an error, however i don't get an error, int main() { int aa=0,b=0; 1>0?aa:b = 10; printf("value %d %d\n",aa,b); return(0); } and output is value 0 0 Thanks in advance ! ! ! In article <f840856d-216c-4bab-ac4a-f962f88a2c3a@h11g2000prf.googlegroups.com>, Rahul <sam_cit@yahoo.co.in> wrote: >Hi Everyone, > > I have the following piece of code, and i expected an error, however >i don't get an error, > >int main() >{ > int aa=0,b=0; > 1>0?aa:b = 10; > ...

Is the following an lvalue or rvalue
I have the folllowing #include <stdio.h> int main(void) { int x = 1, y = 2; int *ip; ip = &x; y = *ip; printf("The value is: %d\n", y); return 0; } Would *ip in y = *ip be a lvalue or rvalue in this case? Here is my main source of confusion. I thought that applying the indirection operator to ip would yield a lvalue and thus something like *ip could only appear on the left hand side. However,since *ip is on the right hand side in this case, would *ip be a lvalue or rvalue? Chad Chad wrote: > I have the folllowing > > #include <stdio.h>...

Converting a temporary object to an lvalue
While I was writing something like ifstream is(filename); string line; getline(is, line); istringstream iss(line); vector<string> words( (istream_iterator<string>(iss)), istream_iterator<string>() ); I didn't like having to declare the object iss, because it would be used only once in the declaration of 'words' and never afterwards. Obviously, istream_iterator<string>(istringstream(line)) doesn't work, because istringstream(line) is a temporary object and would not bind to a non-const refe...

Giving an rvalue ref to a function taking an rvalue ref
Suppose you have a function that takes an rvalue reference as parameter, and from within it you want to call another function that likewise takes one, but there's also a version of that function that takes a regular reference. (This is most often the case with copy/move constructors and assignment operators.) In order to call the version taking the rvalue reference, you have to explicitly re-cast the parameter with std::move (or static_cast<type&&>()). For example, if you were writing a move constructor in a derived class, you would need to write it like: M...

lvalue rvalue
The following compiles: // syrup.cpp struct DoubleInDisguise { double data; }; double Chocolate1() { double blah = 67.22; return blah; } DoubleInDisguise Chocolate2() { DoubleInDisguise blah = { 67.22 }; return blah; } /* inline void Manipulate(double& input) { input = 222.76; } */ inline void Manipulate(DoubleInDisguise& input) { //input.data = 222.76; } int main() { //Manipulate( Chocolate1() ); Chocolate2() = DoubleInDisguise(); // Manipulate( Chocolate2() ); } See how the return-value from Chocolate2() can have an assigment done ...

Verbosity when optimizing with rvalue references
The optimizations that rvalue references make possible are nice, but I'm having one problem with them (rvalue refs): they sometimes lead to too much verbosity. When defining a constructor or a setter, you usually take a const T& and assign it to one of the class's members. In C++0x, you can additionally define an overload that takes a T&&: class OneString { public: OneString(const string& s) : m_s(s) { } OneString(string&& s) : m_s(move(s)) { } private: string m_s; }; One additional overload is not too much...

rvalue-reference result premature destruction
The code shown below produces output: fun<0>(.).val()=100 fun<1>(.).val()=-926642400 when compiled with: ftp://gcc.gnu.org/pub/gcc/snapshots/4.5-20100408 The odd value for fun<1> output is most probably produced by premature destruction of the concrete returned from fun<1> call. How should the code be changed to get output 100 on the 2nd line? TIA. -Larry //== "code shown below" == //Purpose: // See if function can return rvalue-reference of absstract class. // #include <iostream> struct abstract { ...

operators requiring lvalue/rvalue operands and resulting in rvalue/lvalue
These were the questions asked to me by my friend 1. Operator which may require an lvalue operand, yet yield an rvalue. 2. Operator which may require an rvalue operand, yet yield an lvalue. My answer to these questions are & and * respectively. Is/Are there any other operator(s) satisfying these criteria? Kavya wrote: > These were the questions asked to me by my friend > > 1. Operator which may require an lvalue operand, yet yield an rvalue. > 2. Operator which may require an rvalue operand, yet yield an lvalue. > > My answer to these questions are & and * re...

lvalues and rvalues
Hello, I am quite confused about this, so I split it in to subquestions, here we go: 1. Is an lvalue the expression itself, or is it the result yielded by evaluation of an expression? Take this for example: int i = 10; int *p = &i; and then dereference: *p; is the expression "*p" the thing you call an lvalue or is an lvalue the result yielded by evaluating that lvalue-expression? 2. Rvalues, is rvalues the result yielded by an rvalue-expression, or is it the expression itself? If its the result yielded by evaluation of an rvalue-expression is an rvalue a plain val...

Resolving if *this is an rvalue (in C++0x)
One of the major advantages of rvalue references is that they can be used to make code more efficient by utilizing the fact that rvalues (eg. temporaries) can be modified without breaking anything, thus allowing things like move constructors. One typical situation where this may come handy is something like this: Matrix a(1000, 1000), b(1000, 1000); // 1000x1000 unit matrices Matrix c = a + b; Matrix::operator+() will obviously have to allocate a new array for the 1000*1000 elements, where it will put the sum of 'a' and 'b'. But then when 'c' is...