Is it just a mistake????

That lvalue reference when const can reference rvalues or is their a reason????


Or is it just because before c++ 11 it was not possible without rvalue references?

edit: found this for anyone else who gets confused in the future

The important thing is that rvalues refer to temporary objects--just like the value returned from doubleValues. Wouldn't it be great if we could know, without a shadow of a doubt, that a value returned from an expression was a temporary, and somehow write code that is overloaded to behave differently for temporary objects? Why, yes, yes indeed it would be. And this is what rvalue references are for. An rvalue reference is a reference that will bind only to a temporary object. What do I mean?

Prior to C++11, if you had a temporary object, you could use a "regular" or "lvalue reference" to bind it, but only if it was const:

1
2
const string& name = getName(); // ok
string& name = getName(); // NOT ok
The intuition here is that you cannot use a "mutable" reference because, if you did, you'd be able to modify some object that is about to disappear, and that would be dangerous. Notice, by the way, that holding on to a const reference to a temporary object ensures that the temporary object isn't immediately destructed. This is a nice guarantee of C++, but it is still a temporary object, so you don't want to modify it.

In C++11, however, there's a new kind of reference, an "rvalue reference", that will let you bind a mutable reference to an rvalue, but not an lvalue. In other words, rvalue references are perfect for detecting if a value is temporary object or not. Rvalue references use the && syntax instead of just &, and can be const and non-const, just like lvalue references, although you'll rarely see a const rvalue reference (as we'll see, mutable references are kind of the point):
Last edited on
I don't know what exactly your issue is.

const reference works slightly different from it's non const brother.

If a value is provided to a const reference that cannot serve reference, a temporary object is created on the stack and the provided value is assigned to it (if possible).

1
2
3
4
5
6
std::string getName(); // this returns a temporary string
...
const std::string &name = getName(); // This creates a new variable on the stack of the caller function.
// The string returned by getName() will be copied to that variable
// There's no reference to the object returned be getName()
const std::string name = getName(); // This is equivalent 


const reference can act like a normal variable because the value cannot be modified, i.e. the reference has no effect.
While non const reference need to maintain the 'link' mechanism
he is talking about rvalue reference's which are new inclusion to C++'s latest version, came in year 2011.
There is a folder on my desktop named c++11 since more than a year and I didn't get the time to open it yet..!! :-(
Topic archived. No new replies allowed.