Why are some functions within a namespace accessible without the namespace scoping prefix?
c++
Solution
I think you're seeing the effect of Koenig lookup. In your example, `foo` and `s` are types defined in namespace `N`. Your calls to routines `C`, `D`, and `E` use arguments of those types, so namespace `N` is searched in order to resolve those function calls.
Problem
Shouldn't functions within a namespace only be accessible via using the namespace scoping or by the using directive? I am having a problem where certain functions, defined inside of a namespace, are accessible OUTSIDE of that namespace. I believe there should be a compiler error, but I am not getting one across the three different compilers I have tried (VS.NET 2003, VS2010 and GCC 4). Here is the code: ``` 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! } ``` None of the functions should be accessible without using the N:: prefix, but C, D, and E are for some unknown reason. I initially thought it was a compiler bug, but because I am seeing this across multiple compilers it makes me question what is going on.