15

In the following, would there be a temporary object created before const reference is used to a non-const object?

const int y = 2000; const int &s = y // ok, const reference to const object. int x = 1000; const int &r = x; // any temporary copy here? 

If no then, how does this work?

 const int z = 3000; int &t = z // ok, why can't you do this? 

3 Answers 3

21

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.* This doesn't require a copy to be created.

Given that const is merely an instruction to the compiler to enforce "read-only", then it should be immediately obvious why your final example doesn't compile. const would be pointless if you could trivially circumvent it by taking a non-const ref to a const object.

* Of course, you are still free to modify the object via x. Any changes will also be visible via r, because they refer to the same object.

Sign up to request clarification or add additional context in comments.

4 Comments

In particular, the value of r can change, even though it is declared const.
Usually, compilers implement the reference with a simple pointer.
@quant_dev Given OP's sample, the source code that mentions x and source code that mentions r are compiled to identical machine code. No pointers involved (on all compilers I've seen). Not that it's relevant anyway.
@Cubbi: this all depends on the context, whenever the compiler can just alias the names, it will do so, but if the reference is passed to a function or stored in a type, etc. the compiler must store a reference to the object, and that is usually done with pointers.
7

In

int x = 1000; const int &r = x; 

the right-hand side is an lvalue and the type of x is the same as the type of the reference (ignoring cv-qualifications). Under these circumstances the reference is attached directly to x, no temporary is created.

As for "how does this work"... I don't understand what prompted your question. It just works in the most straighforward way: the reference is attached directly to x. Nothing more to it.

You can't do

const int z = 3000; int &t = z; 

because it immediately violates the rules of const-correctness.

2 Comments

so what you are saying is that there is no implicit conversion taking place on x "from int to const int " during reference binding is that right? If yes can you provide link to the standard of the statement which proves your point..
@sparsh goyal: Process of reference initialization is described in 9.4.4/5 (eel.is/c++draft/dcl.init.ref#5). This situation immediately falls under 5.1 and 5.1.1. No conversion "from int to const int" is involved there.
3

The understanding on the Reference (&) answers this question..

Reference is just an alias to the variable that it is assigned to it..

And const is a constraint imposed by the compiler to the variable that is declared as const

int x = 1000; const int &r = x; 

In this case, its a const reference to a non const variable. So you cannot change the data of x with reference variable r(just acts a read only).. yet you can still change the data x by modifying x

const int z = 3000; int &t = z 

In this case, non const reference to const member which is meaningless. You are saying reference can allow you to edit a const member(which is never possible)..

So if you want to create a reference for a const member, it has to be like the first case you mentioned

const int z = 3000; const int &t = z; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.