Reputation: 27395
I'm considering N3797 working draft. There is a quote from 3.3.1/4
friend declarations (11.3) may introduce a (possibly not visible) name into an enclosing namespace
And further in 3.3.2/11 I found
Friend declaration refer to function or classes that are member of the nearest enclosing namespace, but they don't introduce new name into that namespace.
So the name declared by friend declaration is not visible or doesn't introduce at all?
Upvotes: 3
Views: 140
Reputation: 145289
It can be found by Argument Dependent Lookup, but only that way.
E.g. you can implement a comparison operator that way:
struct Point
{
int x, y;
friend
auto operator<( Point const a, Point const b )
-> bool
{
// compare and return true or false
}
};
The name (here operator<
) is not visible in the enclosing scope, but is found when the function is invoked with arguments of type Point
.
The current rules are designed to be backward-compatible with the so called Barton-Nackman trick.
Quoting the Wikpedia article on that:
“When investigating the possibility of removing friend name injection from the C++ programming language, Barton and Nackman’s idiom was found to be the only reasonable use of that language rule. Eventually, the rules for argument-dependent lookup were adjusted to replace friend name injection by a less drastic mechanism, described above, that maintained the validity of Barton and Nackman’s technique”
where “friend name injection” refers to earlier rules where the name did become visible in the enclosing scope.
Upvotes: 4