I'm taking addresses of the following instantiated variable templates from two translation units:
template<class T> bool b = true; template<class T> const bool cb = true; template<class T> inline const bool icb = true; I'm printing addresses of b<int>, cb<int> and icb<int>. Here is what clang says:
0x6030c0 0x401ae4 0x401ae5 // first translation unit 0x6030c0 0x401ae4 0x401ae5 // second translation unit All addresses are the same, kind of expected. And here is what gcc says:
0x6015b0 0x400ef5 0x400ef4 // first translation unit 0x6015b0 0x400ef6 0x400ef4 // second translation unit The address of cb<int> changes. Huh? Is this a bug? If not, could someone please explain this effect to me?
constvariables needs to have addresses at all. If they're not ODR-used, a compiler can optimize them as compile-time constants. And since they are defined in a header file (I assume) there's nothing that requires the variables to end up in the same location in all translation units (since each translation unit have their own instantiation of the templates).