0

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++?

stillanoob
  • 1,279
  • 2
  • 16
  • 29

1 Answers1

4

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 allo­cat­ed instances of type T. (Note also that you are not allowed to apply offsetof to T, since T is not standard-layout.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084