c++ - How can a reference require no storage? -
c++ - How can a reference require no storage? -
from this question, , consequently, standard (iso c++-03):
it unspecified whether or not reference requires storage (3.7).
in answers in thread, it's said references have, internally, same construction of pointer, thus, having same size of (32/64 bits).
what i'm struggling grasp is: how reference come not require storage?
any sample code exemplifying appreciated.
edit: @johannesschaub-litb comment, there like, if i'm not using const &
, or if i'm using const &
default value, requires allocation? seems me, somehow, there should no allocations references @ -- except, of course, when there explicit allocations involved, like:
a& new_reference(*(new a())); // a() instance allocated, // not new_reference
is there case this?
take simple:
int foo() { int x = 5; int& r = x; r = 10; homecoming x; }
the implementation may utilize pointer x
behind scenes implement reference, there's no reason has to. translate code equivalent form of:
int foo() { int x = 10 homecoming x; }
then no pointers needed whatsoever. compiler can bake right executable r
same x
, without storing , dereferencing pointer points @ x
.
the point is, whether reference requires storage implementation detail shouldn't need care about.
c++ variables compiler-construction reference
Comments
Post a Comment