命名空间内的局部函数声明 [英] Local function declaration inside namespace
问题描述
在这种情况下
namespace n {
void f() {
void another_function();
}
}
函数another_function
应该在名称空间n
内部还是外部定义? VS 2012(带有 11月CTP )说它应该在外面,而Mac上的GCC 4.7.2说它应该在里面.如果我做错了,我会从链接器中得到未定义的符号错误.
Should the function another_function
be defined inside the namespace n
or outside? VS 2012 (with the November CTP) says it should be outside, and GCC 4.7.2 on the Mac says it should be inside. If I do the wrong one, I get undefined symbol errors from the linkers.
我通常相信GCC更符合该标准,但这是C ++,您永远不能确定.
I generally trust GCC to be more compliant to the standard, but this is C++ and you can never be sure.
推荐答案
C ++ 11 3.5(以及C ++ 03)
C++11 3.5 (as well as C++03)
7 当未找到具有链接的实体的块范围声明时 引用其他声明,则该实体是的成员 最内层的封闭名称空间.但是,这样的声明并没有 在其名称空间范围内引入成员名称.
7 When a block scope declaration of an entity with linkage is not found to refer to some other declaration, then that entity is a member of the innermost enclosing namespace. However such a declaration does not introduce the member name in its namespace scope.
您的示例中的声明为n::another_function
.
这篇关于命名空间内的局部函数声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!