I'm wondering if there's a memory cost to having multiple "constant reference" variables in the same scope pointing to the same object:
const Animal& animal = getAnimal();
const Dog& dog = static_cast<const Dog&>(animal);
Conceptually, animal and dog are two variables, each of pointer size, and thus would take up 2 registers (or a 2*pointer-size region on the stack).
But (assuming that there's no multiple inheritance etc.), the compiler could know that they both must hold the same pointer value throughout their lifetime.
So, can these two variables share a single register (or a single pointer-sized region on the stack)?
By "can", I mean:
- Does the C++ standard allow it?
- Would modern compilers do it?