Juha said:
Consider the following:
//-----------------------------------------------
class A
{
class B
{
int i;
friend class A;
};
class C
{
void foo(B&);
};
};
void A::C::foo(B& b)
{
b.i = 5; // Allowed or not?
}
//-----------------------------------------------
It seems that gcc and Visual Studio disagree on this (gcc allows it,
Visual Studio issues an error). Which one is right?
The C++03 standard [11.4/2]:
Declaring a class to be a friend implies that the names of private and
protected members from the class granting friendship can be accessed in
declarations of members of the befriended class. [Note: this means that
access to private and protected names is also granted to member functions
of the friend class (as if the functions were each friends) and to the
static data member definitions of the friend class. This also means that
private and protected type names from the class granting friendship can be
used in the base-clause of a nested class of the friend class.
and now for the important sentence:
However, the declarations of members of classes nested within the friend
class cannot access the names of private and protected members from the
class granting friendship.
There is also an example:
Also, because the base-clause of the friend class is not part of its
member declarations, the base-clause of the friend class cannot access the
names of the private and protected members from the class granting
friendship. For example,
class A {
class B { };
friend class X;
};
class X : A::B { // ill-formed: A::B cannot be accessed
// in the base-clause for X
A::B mx; // OK: A::B used to declare member of X
class Y : A::B { // OK: A::B used to declare member of X
A::B my; // ill-formed: A::B cannot be accessed
// to declare members of nested class of X
};
};
]
...
This explanation in in a non-normative a note; and one could argue that the
interpretation that friendship does not extend to inner classes is a little
bit streched given the wording of the normative provision. C++0X has taken
that stance. In n3291, the corresponding clause [11.3/2] reads:
Declaring a class to be a friend implies that the names of private and
protected members from the class granting friendship can be accessed in
the base-specifiers and member declarations of the befriended class.
[ Example:
class A {
class B { };
friend class X;
};
struct X : A::B { // OK: A::B accessible to friend
A::B mx; // OK: A::B accessible to member of friend
class Y {
A::B my; // OK: A::B accessible to nested member of friend
};
};
-- end example ]
...
As one can see from the example, C++ is changing in this regard. That could
explain your observation that compilers disagree on this one.