When you declare f()
as a friend, it actually does in the enclosing namespace of the containing class ( A
in this case) if the forwarding declaration is not already present.
So this is ...
namespace A { class window { private: friend void ::f(window); }; }
essentially becomes this ...
namespace A { class window; void f(window); class window { private: friend void f(window); }; }
Edit: Here is a snippet from the C ++ standard that explains this scenario:
Standard 7.3.1.2/3:
Each name declared in a namespace is a member of this namespace. If a friend declaration in a non-local class first declares a class or function, the friend's class or function is a member of the innermost enclosing namespace. The friend’s name was not found by an unqualified search (3.4.1) or by a qualified search (3.4.3) until a corresponding announcement is displayed in this area of the namespace (before or after the definition of the class that gives friendship).
source share