gpt4 book ai didi

c++ - 限定标识符 C++ 的重复外部声明

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:01:29 25 4
gpt4 key购买 nike

为什么不能在函数内部重新声明限定名称?

以下代码无法编译(在 MSVC2015 和 clang 中)

int i;

namespace N
{
int j;
}

void foo()
{
extern int i;
extern int i;
extern int N::j;
extern int N::j;
}

int main()
{
return 0;
}

但是,如果我们将两行 extern int N::j; 移动到 void foo() 之前,那么代码可以正常编译。


更新 值得注意的是

  1. 重复声明不合格的名称确实有效,
  2. ::i::N::j 都已经在它们各自的声明中定义了,
  3. ::N::jfoo
  4. 不可见
  5. 语法确实允许 extern int::N::j 并且它不是定义
  6. 下面的代码也失败了(感谢 T.C. 指出这一点)

.

int i;
void foo()
{
extern int ::i;
}
  1. 以下代码在 MSVC 中有效,但在 clang 中给出警告

.

int i;
extern int ::i;

最佳答案

T.C.指示我到 [dcl.meaning]p1

… When the declarator-id is qualified, the declaration shall refer to a previously declared member of the class or namespace to which the qualifier refers (or, in the case of a namespace, of an element of the inline namespace set of that namespace (7.3.1)) or to a specialization thereof …

在我看来,这是一个标准没有如实地体现在实现中的案例。

关于c++ - 限定标识符 C++ 的重复外部声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31729985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com