Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const reference can be assigned an int?

I came across a code snippet

const int& reference_to_const_int = 20;
cout<<"\n  reference_to_const_int = "<<reference_to_const_int<<endl;     

This code compiles & executes with output :-

reference_to_const_int = 20

This is something strange in for me. As I know reference do not occupy memory & they are aliases to other variables. hence we cannot say

int& reference_to_int = 30;

The above statement shall not compile giving error :-

 error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’

What exactly is happening in the "const int&" case? A full explanation is desired.

Kindly help.

Thanks

like image 284
AnotherDeveloper Avatar asked May 29 '12 15:05

AnotherDeveloper


People also ask

What does const reference do?

Passing by const reference allows changes to the original object to be visible inside the function.

Can a reference value be reassigned?

Once defined, a reference cannot be reassigned because it is an alias to its target. What happens when you try to reassign a reference turns out to be the assignment of a new value to the target. Because arguments of a function are passed by value, a function call does not modify the actual values of the arguments.

Can you modify a const reference C++?

But const (int&) is a reference int& that is const , meaning that the reference itself cannot be modified.

What does constant reference mean?

A constant reference is an expression that evaluates to the value of the named constant. The simplest constant references are primary expressions—they consist simply of the name of the constant: CM_PER_INCH = 2.54 # Define a constant. CM_PER_INCH # Refer to the constant.


2 Answers

A temporary is created, and it's legal to bind a const reference to it, but illegal to bind it to a non-const one.

It's just like:

const int& reference_to_const_int = int(20);  //LEGAL
      int& reference_to_const_int = int(20);  //ILLEGAL

A const reference extends the life of a temporary, that's why this works. It's just a rule of the language.

like image 198
Luchian Grigore Avatar answered Sep 24 '22 13:09

Luchian Grigore


This behavior is easier to understand when we look at what happens when we bind a reference to a temporary object. If we write

const int& reference_to_const_int = 20; //A temporay object int(20) is created.

the compiler transforms above code into something like this:

int temp = 20;
const int& reference_to_const_int = temp;

If reference_to_const_int were not const, then we could assign a new value to reference_to_const_int. Doing so would not change literal 20 but would instead change temp, which is a temporary object and hence inaccessible. Allowing only const references to be bound to values requiring temporaries avoids the problem entirely because a const reference is read-only.

Why do C++ allows const references to accept temporary objects or RVALUES (like literals)?

The most common places we see references are as function arguments or return values. When a reference is used as a function argument, any modification to the reference inside the function will cause changes to the argument outside the function.

If function can expect/accept temporary objects or literals as inputs and if the function respects const-ness of the object, making the argument a const reference will allow the function to be used in all situations.

Temporary objects are always const, so if you don’t use a const reference, that argument won’t be accepted by the compiler.

void f(int&) {}
void g(const int&) {}
int main() 
{
    //f(1); //Error
    g(1); //OK 
}
like image 43
Vaibhav Patle Avatar answered Sep 24 '22 13:09

Vaibhav Patle