Suppose I have a class F that should be friend to the classes G (in the global namespace) and C (in namespace A).
- to be friend to
A::C,Fmust be forward declared. - to be friend to
G, no forward declaration ofFis necessary. - likewise, a class
A::BFcan be friend toA::Cwithout forward declaration
The following code illustrates this and compiles with GCC 4.5, VC++ 10 and at least with one other compiler.
class G {
friend class F;
int g;
};
// without this forward declaration, F can't be friend to A::C
class F;
namespace A {
class C {
friend class ::F;
friend class BF;
int c;
};
class BF {
public:
BF() { c.c = 2; }
private:
C c;
};
} // namespace A
class F {
public:
F() { g.g = 3; c.c = 2; }
private:
G g;
A::C c;
};
int main()
{
F f;
}
To me this seems inconsistent. Is there a reason for this or is it just a design decision of the standard?