What the standard says:
It is not indicated whether reference (3.7) is required for storage.
(C ++ 11, [dcl.ref] ΒΆ4)
This means that the compiler can choose for each case, regardless of whether any storage is required.
Now let people say what they want, but links come down to syntactic sugar for pointers (even at the compiler level in all major C ++ compilers, the concept of βlinkβ disappears almost immediately after the interface); thus, in the general case, they may need their space in memory, just like a pointer. However, in cases such as yours (local links), the compiler should look at them and optimize them as necessary.
Please note that this is not an exception to the links - the compiler is able to perform the same optimization even with pointers (as soon as your code goes into SSA, there is nothing special even if the links cannot be reinstalled).
It:
int glob; void direct() { glob = 16; } void through_reference() { int &a = glob; a = 16; } void through_pointer() { int *a = &glob; *a = 16; }
always boils down to the same code on any compiler I tried on gcc.godbolt.org - an example :
direct(): mov DWORD PTR glob[rip], 16 ret through_reference(): mov DWORD PTR glob[rip], 16 ret through_pointer(): mov DWORD PTR glob[rip], 16 ret glob: .zero 4
On the other hand, the situation becomes a little more slippery when it comes to structures; here, the compiler is allowed to kill links away from the actual location of the structure (if it is able to restore what they actually point to), while for pointers the situation can be a little more complicated (their elite will violate the classes of the standard layout material).
In practice, I have never seen such optimization implemented in any real compiler. Take gcc or MSVC or Clang or something else, and you will always see that the size of the structure will be equal even in the most trivial cases .