A function can be defined in a friend declaration of a class if and only if the class is a non-local class ([class.local]), the function name is unqualified, and the function has namespace scope. [...]
What does "the function has namespace scope" mean?
The only situation that I could come up with in which the function does not have namespace scope is the following:
struct A
{
static void f();
struct B
{
friend void f() {};
};
};
However neither clang nor gcc associate the friend definition inside B
to the static method inside A
, the friend definition is associated to a function that belongs to the global namespace.
"[...] 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. [...]"
So,
- is the condition "the function has namespace scope" in [class.friend] p.6 redundant and [namespace.memdef] p.3 must be read as: "the lookup is done only in the innermost enclosing namespace's scope"
- or is the condition correct, [namespace.memdef] p.3 is read as "the lookup is done in all scopes between the declaration and the innermost enclosing namespace (but not outside the innermost enclosing namespace" and the compilers are wrong?
struct A
{
static void f();
struct B
{
friend void f() {}
void g()
{
f(); // resolves to A::f(), which hides ::f()
}
};
};