c++ - 编译器差异 : Interaction between alias resolution and name lookup

标签 c++ c++11 language-lawyer typedef name-lookup

考虑这段代码:

using type = long;

namespace n {
  using type = long;
}

using namespace n;
int main() {
  type t;
}

这可以在 Clang 3.7GCC 5.3 上干净地编译,但是 MSVC 19 * 给出以下错误消息:

main.cpp(9): error C2872: 'type': ambiguous symbol
main.cpp(1): note: could be 'long type'
main.cpp(4): note: or       'n::type'

这段代码格式是否正确?标准的哪一部分说明在歧义检查之前是否已解析别名?


请注意,如果您更改其中一个别名,Clang 和 GCC 都会给出与 MSVC 类似的错误。

我完全知道限定名称将如何解决歧义,我只是对标准对此有何评论感兴趣。


*- 只需粘贴代码并在该链接运行它,我不知道是否有带有永久链接的在线 MSVC 编译器

最佳答案

[namespace.udir]/6 :

If name lookup finds a declaration for a name in two different namespaces, and the declarations do not declare the same entity and do not declare functions, the use of the name is ill-formed.

但是,它们确实声明了指向同一类型的名称,因此程序应该是良构的。这种解释是例如由 core issue 1894 中的评论确认:

  //[..]

  namespace C {
    // The typedef does not redefine the name S in this
    // scope, so issue 407's resolution does not apply.
    typedef A::S S;
    using A::S;
    // **The name lookup here isn't ambiguous, because it only finds one
    // entity**, but it finds both a typedef-name and a non-typedef-name referring
    // to that entity, so the standard doesn't appear to say whether this is valid.
    struct S s;
  }

关于c++ - 编译器差异 : Interaction between alias resolution and name lookup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36204506/

相关文章:

c - strtoull ("-1", NULL, 0) 和其他负值的惊人行为

Python C API : T_INT was not declared in this scope

c++ - 读取文件,尝试返回二维字符数组

c++ - C++ 中的 Pause()、Sleep() 和 Wait() 有什么区别?

c++ - C++11 chrono 有哪些替代方案?

c++ - 创建指向成员函数的指针映射

c++ - 为什么要为未删除的对象调用析构函数?

arrays - 数组可以有尾部填充吗?

C++ 内存缓存库

c++ - 制作键盘记录器