Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-const reference may only be bound to an lvalue

Tags:

c++

Could someone please explain how to simulate this error? and also what this complains about.

"A non-const reference may only be bound to an lvalue" in C++

like image 777
Pavan Dittakavi Avatar asked Aug 06 '11 15:08

Pavan Dittakavi


People also ask

What is non const reference?

Whether a reference refers to a const or nonconst type affects what we can do with that reference, not whether we can alter the binding of the reference itself." I think this means that making a reference a "const" when it is referenced to a non const object does absolutely nothing.

Can a const reference be bound to a non const object?

No. A reference is simply an alias for an existing object. const is enforced by the compiler; it simply checks that you don't attempt to modify the object through the reference r .

Is const reference an lvalue?

Such a reference is called an lvalue reference to a const value (sometimes called a reference to const or a const reference). In the above program, we bind const reference ref to modifiable lvalue x . We can then use ref to access x , but because ref is const, we can not modify the value of x through ref .

Why can't you make a non const reference to a literal?

A non-const reference cannot point to a literal. You cannot bind a literal to a reference to non-const (because modifying the value of a literal is not an operation that makes sense) and only l-values can be bound to references to non-const.


2 Answers

An lvalue is, roughly, whatever may be on the left side of an assignment statement. References provide aliases for other objects:

std::string s;
std::string & rs = s;  // a non-const reference to s
std::string const & crs = s; // a const reference to s

Given the above definitions, referring to rs or crs is the same as referring to s, except that you cannot modify the referred string through crs, as it is const. A variable is an lvalue, so you are allowed to bind a non const reference to it. In contrast you can bind const references to temporary values as in:

std::string const & crs1 = std::string();

However the following is illegal:

std::string & rs1 = std::string();

This is because using non-const references you imply that you want to modify the referenced object. However temporaries bound to references are destroyed when the reference go out of scope. As it is not always intuitive when C++ creates temporary objects, binding them to non-const references has been disallowed, to avoid you the unpleasant surprise of changing your object as you like, just to see it destroyed a few statements later.

like image 60
Nicola Musatti Avatar answered Nov 15 '22 17:11

Nicola Musatti


It means you can't do something like this:

void foo(int &x) { ... }
int bar()        { ... }

foo(bar());

You would need to make foo take a const reference, or assign the result of bar() to a variable, and then pass that into into foo.

like image 43
Oliver Charlesworth Avatar answered Nov 15 '22 18:11

Oliver Charlesworth