From the C++ Standard
11.3 Friends
6 A function can be defined in a friend declaration of a class if and
only if the class is a non-local class (9.8), the function name is
unqualified, and the function has namespace scope. [ Example:
class M { friend void f() { } // definition of global f, a friend of M,
// not the definition of a member function
};
—end example ]
Amd the other quote (7.3.1 Namespace definition)
3 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, function, class template or function template98 the friend is
a member of the innermost enclosing namespace. The friend declaration
does not by itself make the name visible to unqualified lookup (3.4.1)
or qualified lookup (3.4.3). [ Note: The name of the friend will be
visible in its namespace if a matching declaration is provided at
namespace scope (either before or after the class definition granting
friendship). —end note ] If a friend function or function template is
called, its name may be found by the name lookup that considers
functions from namespaces and classes associated with the types of the
function arguments (3.4.2). If the name in a friend declaration is
neither qualified nor a template-id and the declaration is a function
or an elaborated-type-specifier, the lookup to determine whether the
entity has been previously declared shall not consider any scopes
outside the innermost enclosing namespace. [ Note: The other forms of
friend declarations cannot declare a new member of the innermost
enclosing namespace and thus follow the usual lookup rules. —end note
]
I would like to mention that enclosing a function name in parentheses suppresses the argument-dependent lookup.
For this code
int main()
{
A::X x;
( f )(x);
}
function f will not be found.