I’m not a compiler guy so this could be partially or completely wrong, but
const can’t refer to
statics at all simply because
statics don’t have addresses yet when const-expansion occurs. This is because, while they seem to be roughly equivalent constructs,
const is an abstraction while
static is a concrete memory location.
Every use of a
const expands to its rvalue, including when used as initialization in other
consts. Whereas every
static gets its own unique location in memory that is initialized during translation. You can see this difference when trying to take a
const and a
static by-value: http://is.gd/X9AwL
You can re-use the
const by-value as many times as you like because it’s essentially a copy-paste, whereas you can’t use the
static by-value because you’re trying to move from a location in memory.
You can take references to
consts in other
statics because they all const-expand to rvalues which will get filled in during translation, and you can take references to
statics in other
statics because they get filled in during translation, but you can’t reference
consts because they don’t have a memory location yet.
Have a look at this modification of your original example: http://is.gd/zsSjVE
If you run it, you’ll see the same pointer value gets printed for all three expressions, because they all reference the same
Whereas in this version: http://is.gd/gSki69
&Y reference unique locations in the data segment; interestingly, with a 12 byte offset. I wonder what the compiler is putting between those two. Doesn’t matter for our purposes, but interesting nonetheless.