Consider the following:
namespace N {
struct A { };
struct B {
B() { }
B(A const&) { }
friend void f(B const& ) { }
};
}
int main() {
f(N::B{}); // ok
f(N::A{}); // error
}
In the first case, the case succeeds - we consider the associated namespaces of N::B
and find N::f(B const&)
. Great.
The second case fails. Why? According to [namespace.memdef]:
If a
friend
declaration in a non-local class first declares a class, function, class template or function template the friend is a member of the innermost enclosing namespace. [...] 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).
The associated namespace of N::A
is N
, of which f
is a member, so why is it not found by lookup?