thing.hpp:
struct
{
} inline thing;
other.cpp:
#include "thing.hpp"
#include <cstdio>
void test()
{
std::printf("test: %p\n", &thing);
}
main.cpp:
#include "thing.hpp"
#include <cstdio>
extern void test();
int main()
{
std::printf("main: %p\n", &thing);
test();
}
Compiling with Clang 11 or GCC 9.3.0 in C++17 mode the output is something like this:
main: 0x404031
test: 0x404032
Per my understanding inline variables with the same definition should have the same address in every translation unit.
Giving the struct type a name results in the addresses being the same.
Why does thing not have the same address in different translation units?