编译器差异:别名解析和名称查找之间的交互

Tar*_*ama 10 c++ typedef language-lawyer name-lookup c++11

考虑以下代码:

using type = long;

namespace n {
  using type = long;
}

using namespace n;
int main() {
  type t;
}
Run Code Online (Sandbox Code Playgroud)

这在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'
Run Code Online (Sandbox Code Playgroud)

这段代码是否格式良好?标准的哪一部分注意到在模糊检查之前是否解决了别名?


请注意,如果更改其中一个别名,Clang和GCC都会向MSVC提供类似的错误.

我完全清楚这个名字的合格性如何解决这种模糊性,我只是对标准对此有何看法感兴趣.


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

Col*_*mbo 4

[命名空间.udir]/6 :

如果名称查找在两个不同的命名空间中找到名称的声明,并且这些声明没有声明相同的实体,也没有声明函数,则该名称的使用格式不正确。

然而,它们确实声明了引用相同类型的名称,因此程序应该是格式良好的。例如,核心问题 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;
  }
Run Code Online (Sandbox Code Playgroud)