c++ - 执行限定名称查找时 Clang 和 GCC 之间的不同行为

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

考虑以下程序:

#include <iostream>

namespace N {
    int j = 1;
}

namespace M {
    typedef int N;
    void f() {
        std::cout << N::j << std::endl;
    }
}

int main() { M::f(); }

用 clang 编译它会出现以下编译器错误:

prog.cc:10:22: error: 'N' (aka 'int') is not a class, namespace, or
enumeration
    std::cout << N::j << std::endl;
                 ^ 1 error generated.

GCC 不会给出任何编译器错误。我想弄清楚我应该为哪个编译器提交错误报告。哪个编译器具有正确的行为以及原因(对 c++ 标准的引用)?

Wandbox - Clang:http://melpon.org/wandbox/permlink/s0hKOxCFPgq5aSmJ

Wandbox - GCC:http://melpon.org/wandbox/permlink/i2kOl3qTBVUcJVbZ

最佳答案

Clang 在这一点上是正确的。引用 C++11,3.4.3/1 [basic.lookup.qual]:

... If a :: scope resolution operator in a nested-name-specifier is not preceded by a decltype-specifier, lookup of the name preceding that :: considers only namespaces, types, and templates whose specializations are types. If the name found does not designate a namespace or a class, enumeration, or dependent type, the program is ill-formed.

根据这个子句,在查找期间应该考虑类型,因此应该找到 typedef N。由于它没有指定 namespace 、类、枚举或依赖类型,因此该程序格式错误。

关于c++ - 执行限定名称查找时 Clang 和 GCC 之间的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31464771/

相关文章:

c++ - 删除全局指针

c++ - 我应该如何使用所有不同的链表格式化命名空间?

c++ - 使用 typedef 创建对数组的引用

c++ - 将 typedef 与模板类一起使用

c++ - 如何为内联变量模板创建自定义点?

C++ 控制台 Application1.exe 已触发断点

c++ - 无法将我的解决方案中的其他项目与 premake 链接起来

没有命名空间的 C# XElement

asp.net-mvc - WebForms 和 ASP.NET MVC 共存

c - 将类型定义的二维结构数组作为函数参数传递