命名空间中的函数不应该只能通过使用命名空间范围或使用指令来访问吗?
我遇到了一个问题,即在命名空间内定义的某些函数可以在该命名空间之外访问。我相信应该有一个编译器错误,但我没有在我尝试过的三种不同编译器(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 出于某种未知原因。我最初认为这是一个编译器错误,但因为我在多个编译器中看到了这一点,这让我质疑发生了什么。