const引用是否在C++中具有外部链接?

spo*_*ang 5 c++ compiler-construction linker

根据C++ 1998标准第3.5节第3节,const引用具有内部链接.

具有命名空间作用域(3.3.5)的名称具有内部链接(如果它的名称)

  • 显式声明为static的对象,引用,函数或函数模板,

  • 显式声明为const的对象或引用,既未显式声明为extern,也未声明为具有外部链接; 要么

  • 匿名联盟的数据成员.

但是,为什么在编译以下代码时会产生多个定义冲突?

// a.cpp
const int& a = 1;

int main()
{
    return 0;
}

// b.cpp
const int& a = 1;
Run Code Online (Sandbox Code Playgroud)

然后编译代码.

$ 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
Run Code Online (Sandbox Code Playgroud)

如果const引用更改为const,如下所示

// a.cpp
const int a = 1;

int main()
{
    return 0;
}

// b.cpp
const int a = 1;
Run Code Online (Sandbox Code Playgroud)

编译是可以的.

Mik*_*our 2

引用本身不是const,只是它引用的对象;所以(可以说)这条规则没有给出参考内部链接。

声明引用是没有意义的const。C++11标准澄清了这样的措辞:

显式声明的变量const,或者constexpr既没有显式声明extern也没有先前声明的具有外部链接的变量

没有提及声明的引用的荒谬概念const