内联命名空间变量是否具有内部链接?如果没有,为什么下面的代码工作? [英] Do inline namespace variables have internal linkage? If not, why does the code below work?
问题描述
此问题与此一直接相关。考虑代码:
This question is directly related to this one. Consider the code:
#include <iostream>
inline namespace N1
{
int x = 42;
}
int x = 10;
int main()
{
extern int x;
std::cout << x; // displays 10
}
显示 10
。如果我删除 extern int x;
声明,那么我们得到一个歧义编译器时间错误
It displays 10
. If I remove the extern int x;
declaration then we get an ambiguity compiler time error
错误:对x的引用不明确
error: reference to 'x' is ambiguous
问题:为什么代码与 extern int x
声明工作,为什么它停止工作,当我删除它?是因为内联命名空间变量有内部链接?
Question: why does the code work with the extern int x
declaration work, and why does it stop working when I remove it? Is it because inline namespace variables have internal linkage?
推荐答案
否。在[basic.link]中没有规定会导致 x
具有内部链接。具体来说,所有其他命名空间具有外部链接,其他是指未命名。也许你在考虑未命名的命名空间?
No. There is no provision in [basic.link] that would cause x
to have internal linkage. Specifically, "All other namespaces have external linkage.", and "other" refers to "not unnamed". Perhaps you were thinking of unnamed namespaces?
这篇关于内联命名空间变量是否具有内部链接?如果没有,为什么下面的代码工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!