initialize a reference with an rvalue

c++ moderated

initialize a reference with an rvalue

Postby Marlene Miller » Sat, 16 Oct 2004 06:50:32 GMT

My compiler does something I didn't expect. So I need to check what I
understand.

struct S { int x; };

S f() { return S(); }

void g() {
 S& r1 = S();  // rvalue
 S& r2 = f();  // rvalue
 S& r3 = *(new S());  // lvalue
 delete &r3;
}

(1) Is the initializer expression S() an rvalue?
(2) Is the initializer expression f() an rvalue?
(3) Is the initializer expression *(new S()) an lvalue?
(4) This compiles OK. But should r1 and r2 be const?

Thank you,
Marlene


      [ See  http://www.**--****.com/ ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

Re: initialize a reference with an rvalue

Postby kanze » Sun, 17 Oct 2004 07:02:32 GMT









Yes.


Yes.


Yes.


Yes.

The restriction that initializers of non-const references had to be
lvalues was an innovation, introduced around 15 years ago.  Some
compilers still aren't up to date concerning it, however, and a lot will
only generate a warning, rather than an error, in order to avoid
breaking existing code.

      [ See  http://www.**--****.com/ ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


Re: initialize a reference with an rvalue

Postby Catalin Pitis » Sun, 17 Oct 2004 07:13:40 GMT

The second statement:

S& r2 = f();

is not ok. r2 will have a reference to a temporary object, that is destroyed 
when after the statement is executed. Using r2 later will crash the 
application (depending on how lucky you are :)

Catalin






      [ See  http://www.**--****.com/ ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


Re: initialize a reference with an rvalue

Postby Catalin Pitis » Sun, 17 Oct 2004 07:20:53 GMT

An I forgot the first statement:

 S& r1 = S();

It has the same problem: refering a temporary object, that will be destroyed 
after the reference is set.

Catalin (once again)






      [ See  http://www.**--****.com/ ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

Re: initialize a reference with an rvalue

Postby hosoda » Wed, 20 Oct 2004 02:26:22 GMT






IMHO, a const reference can refer to a temporary.

      [ See  http://www.**--****.com/ ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


Similar Threads:

1.Confused on rvalue, lvalue and rvalue references

Hi,

I know traditinal meanings of rvalue and lvalue. lvalue means "can be placed 
on the left or right side of an assignment" and rvalue means "can be placed 
only on the right side of an assignment". So,

int a = 0, b = 0, c = 9;
a = b + c

In the above code, a is an lvalue and (b + c) is an rvalue expression. But I 
am confused about what object rvalue and rvalue reference is? what is 
important about being "r" or lvalue, move semantics and perfect forwarding?

I need a clean explanation.

Thanks in advance.



2.Rvalue Reference question: Overloading on whether *this is an rvalue

3.Lvalue-to-rvalue conversion vs rvalue reference

4.The real problem with allowing build-in rvalue reference to be modified, Was: (N2118)Rvalue reference may create a loophole in the type system

5.rvalue reference questions

6. rvalues and constant reference to a pointer ?

7. rvalue references and default constructors

8. rvalues and constant reference to a pointer ?



Return to c++ moderated

 

Who is online

Users browsing this forum: No registered users and 6 guest