Since a reference is not an object in its own and hence, cannot be taken address of (unlike pointers which are proper objects with storage allocated to them), then how or where references are stored or kept track of in c++?
-
2The storage of references is unspecified. – Kerrek SB Jun 05 '16 at 09:30
-
A reference does not need to be "stored", much like a variable name does not need to be "stored". – Christian Hackl Jun 05 '16 at 09:37
1 Answers
From [dcl.ref]/4:
It is unspecified whether or not a reference requires storage (3.7).
Note however that storage duration applies to references just was well as to objects: From [basic.stc]/3:
The storage duration categories apply to references as well.
That is, the formal life-time constraints on variables apply to all variables equally, whether they are objects or references, and a reference can go out of scope and its lifetime ends just like for other variables.
However, there can be no "dynamic refences", so the dynamic storage duration doesn't apply. Second, storage duration also applies to values of expressions, and of course the value of an expression is never a reference (but always an object).
Finally, a non-static reference data member is stored as part of the object which contains it. So you can perfectly well have struct T { int & r; };
and have temporaries and dynamically allocated instances of type T
. (Note also that you are not allowed to apply offsetof
to T
, since T
is not standard-layout.)

- 464,522
- 92
- 875
- 1,084
-
[Dirty demo](https://ideone.com/RMP3wz) (see also [here](http://stackoverflow.com/q/37409590)). – Kerrek SB Jun 05 '16 at 09:41