只有通过使用命名空间作用域或using指令,才能访问命名空间中的函数吗?
我遇到的问题是,在命名空间内定义的某些函数可以在该命名空间的OUTSIDE中访问。我相信应该有一个编译器错误,但我没有在我尝试过的三个不同的编译器中得到一个(VS.NET 2003,VS2010和GCC 4)。
以下是代码:
namespace N{
typedef struct _some_type *some_type;
struct some_struct { int x; };
void A(void);
void B(int);
void C(some_type*);
void D(some_type);
void E(struct some_struct);
}
using N::some_type;
using N::some_struct;
void TestFunction()
{
some_type foo;
some_struct s;
N::A(); //should compile (and does on VS2003, VS2010, and GCC 4.1.2)
::A(); //shouldn't compile (and doesn't on VS2003, VS2010, and GCC 4.1.2)
A(); //shouldn't compile (and doesn't on VS2003, VS2010, and GCC 4.1.2)
N::B(0); //should compile (and does on VS2003, VS2010, and GCC 4.1.2)
::B(0); //shouldn't compile (and doesn't on VS2003, VS2010, and GCC 4.1.2)
B(0); //shouldn't compile (and doesn't on VS2003, VS2010, and GCC 4.1.2)
N::C(&foo); //should compile (and does on VS2003, VS2010, and GCC 4.1.2)
::C(&foo); //shouldn't compile (and doesn't on VS2003, VS2010, and GCC 4.1.2)
C(&foo); //shouldn't compile (but does on VS2003, VS2010, and GCC 4.1.2) -- problem!
N::D(foo); //should compile (and does on VS2003, VS2010, and GCC 4.1.2)
::D(foo); //shouldn't compile (and doesn't on VS2003, VS2010, and GCC 4.1.2)
D(foo); //shouldn't compile (but does on VS2003, VS2010, and GCC 4.1.2) -- problem!
N::E(s); //should compile (and does on VS2003, VS2010, and GCC 4.1.2)
::E(s); //shouldn't compile (and doesn't on VS2003, VS2010, and GCC 4.1.2)
E(s); //shouldn't compile (but does on VS2003, VS2010, and GCC 4.1.2) -- problem!
}
如果不使用N ::前缀,则无法访问任何函数,但C,D和E出于某种未知原因。我最初认为这是一个编译器错误,但因为我在多个编译器中看到这个,它让我怀疑发生了什么。
答案 0 :(得分:11)
我认为你看到了Koenig lookup的影响。在您的示例中,foo
和s
是类型
在命名空间N
中定义。您对例程C
,D
和E
的调用使用这些类型的参数,因此搜索命名空间N
以解析这些函数调用。