2

I have the simple code:

int & i = *(new int(100));

As far as I know, the RHS of the equation is a rvalue.. How can an rvalue bound to a lvalue reference?

Also if this is legal, does it produce the desired result always, or it is an undefined behavior? And if I do delete &i, will it be an undefined behavior too?

SwiftMango
  • 15,092
  • 13
  • 71
  • 136
  • Asked yesterday... ^^ –  Nov 11 '13 at 07:39
  • @H2CO3 I saw that question and left a comment of this question but no one answered so I created my own question. I am asking more fundamentals in terms of value types, which were not discussed in the answers in that post. – SwiftMango Nov 11 '13 at 07:41

1 Answers1

1

The right-hand side of the assignment is a dereferenced pointer, and this is an lvalue, so there no problem binding an lvalue reference to it. The standard specifically gives the example of a dereferenced pointer in its definition of an lvalue (3.10/1):

An lvalue (so called, historically, because lvalues could appear on the left-hand side of an assignment expression) designates a function or an object. [ Example: If E is an expression of pointer type, then *E is an lvalue expression referring to the object or function to which E points. As another example, the result of calling a function whose return type is an lvalue reference is an lvalue. — end example ]

There is no undefined behavior because i refers to a valid object.

Your question about delete is covered here.

Community
  • 1
  • 1
interjay
  • 107,303
  • 21
  • 270
  • 254