There is a quote from 3.4.1/7:
When looking for a prior declaration of a class or function introduced by a friend declaration, scopes outside of the innermost enclosing namespace scope are not considered;
Can you get an example to demonstrate this rule?
Sure. This code works (both classes are in the same namespace):
namespace Foo {
class Bar
{
friend class FooBar;
public:
Bar() : i(0){}
private:
int i;
};
class FooBar
{
FooBar( Bar & other )
{
other.i = 1;
}
};
}//namespace Foo
And this code fails (the friend class is outside of the Foo
's enclosing namespace, thus the lookup fails and you see the the int Foo::i is private within this context
error):
namespace Foo {
class Bar
{
friend class FooBar;
public:
Bar() : i(0){}
private:
int i;
};
}//namespace Foo
class FooBar
{
FooBar( Foo::Bar & other )
{
other.i = 1;//Oops :'(
}
};