According to clause 3 of section 3.5 of C++ 1998 standard, a const reference has internal linkage.
A name having namespace scope (3.3.5) has internal linkage if it is the name of
an object, reference, function or function template that is explicitly declared static or,
an object or reference that is explicitly declared const and neither explicitly declared extern nor previously declared to have external linkage; or
a data member of an anonymous union.
But why multiple definition conflict is generated when compiling the following code?
// a.cpp const int& a = 1; int main() { return 0; } // b.cpp const int& a = 1; And then compile the code.
$ g++ a.cpp b.cpp /tmp/ccb5Qi0M.o:(.bss+0x0): multiple definition of `a' /tmp/ccD9vrzP.o:(.bss+0x0): first defined here collect2: error: ld returned 1 exit status If the const reference is changed to const, as follows
// a.cpp const int a = 1; int main() { return 0; } // b.cpp const int a = 1; It is OK to compile.