Every name first declared in a namespace is a member of that namespace.
If a friend declaration in a non-local class first declares a class
or function, the friend class or function is a member of the innermost
enclosing namespace.
The following is an example of this structure:
// f has not yet been defined void z(int); namespace A { class X { friend void f(X); // A::f is a friend }; // A::f is not visible here X x; void f(X) { /* definition */} // f() is defined and known to be a friend } using A::x; void z() { A::f(x); // OK A::X::f(x); // error: f is not a member of A::X }
In this example, function f() can only be called through namespace A using the call A::f(s);. Attempting to call function f() through class X using the A::X::f(x); call results in a compiler error. Since the friend declaration first occurs in a non-local class, the friend function is a member of the innermost enclosing namespace and may only be accessed through that namespace.
Related References