“使用命名空间"匿名名称空间中的语句 [英] "using namespace" statement inside an anonymous namespace
问题描述
在匿名名称空间中使用using namespace
语句时,将使用的名称空间带入文件范围吗?例如:
When using a using namespace
statement inside an anonymous namespace bring the namespace used in to the file scope? Eg:
namespace foo
{
int f() { return 1; }
}
namespace
{
using namespace foo;
}
int a()
{
return f(); // Will this compile?
}
推荐答案
根据7.3.4 [namespace.udir]第4段,名称空间指令是可传递的:
According to 7.3.4 [namespace.udir] paragraph 4 a namespace directive is transitive:
对于不合格的查找,它会提名另一个本身包含using-directives的名称空间,其效果就好像第二个名称空间中的using-directives也出现在第一个名称空间中.
For unqualified lookup nominates a second namespace that itself contains using-directives, the effect is as if the using-directives from the second namespace also appeared in the first.
...,根据7.3.1.1 [namespace.unnamed]段落1,未命名的命名空间有一种隐式using指令:
... and according to 7.3.1.1 [namespace.unnamed] paragraph 1 there is kind of an implicit using directive for the unnamed namespace:
未命名空间定义的行为就像被
An unnamed-namespace-definition behaves as if it were replaced by
内联名称空间唯一{/*空主体*/}
使用唯一的名称空间;
名称空间唯一{{namespace-body}
inline namespace unique { /* empty body */ }
using namespace unique ;
namespace unique { namespace-body }
仅当内联出现在unnamed-namespace-definition中时,内联出现在翻译单元中的所有出现的唯一性都由相同的标识符替换,并且此标识符与整个程序中的所有其他标识符不同.
where inline appears if and only if it appears in the unnamed-namespace-definition, all occurrences of unique in a translation unit are replaced by the same identifier, and this identifier differs from all other identifiers in the entire program.
因此,答案是是的,应该编译"(它与我尝试过的所有C ++编译器都兼容).
Thus, the answer is "yes, this is supposed to compile" (and it does with all C++ compilers I tried it with).
这篇关于“使用命名空间"匿名名称空间中的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!