Is it right to do so:
void f();
namespace ns
{
class C
{
int m;
friend void ::f();
};
}
void f()
{
ns::C c;
c.m = 2;
}
?
The code is compiled successfully in VS. But I can't find anything about qualifying a namespace for a friend declaration in the standard. Is it even legal? Can someone provide the link or the quote from the standard?
Yes it's legal.
A namespace
is little more than a way of adding another layer of descrimination for functions, classes, &c..
Any function can be marked as a friend
inside a class
, even if that class
is in a different namespace
to the function. The function could be in a namespace
too.
There's no specific section in the standard on this, but it's a corollary of the standard. This reference is a starting point: https://en.cppreference.com/w/cpp/language/namespace