考虑以下程序:
#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/