I've wrote some code which failed to compile by gcc-4.4.1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//here was mistake - struct written instead of class
class Access
{
friendclass Test;
struct Tag
{};
};
template <typename AccessTag>
struct Foo
{};
class Test
:public Foo<Access::Tag> // error: Access::Tag is private within this context
{
Foo<Access::Tag> tag; // compiled ok
};
Can't figure out is this really error, or just a bug in compiler.
Ok, now gcc 3.3.3 and 4.1.2 both winge, but clang still compiles cleanly. I'd trust clang more than gcc,
and the code looks right to me, so I'd say it is a problem with gcc.
"It seems that the friendship is not occurring until the open brace on struct Test."
Yes, and i can't still understand what the standard says about such cases.. Code looks right to me too.
boolivar: By declaring Test to be a friend of Access (line 3 of your first code block above), Test
should have access to Access' private elements. That is what friendship does.
For comparison, take my "simplest example" code above, remove the ": Access::tag" from line 7,
and add between lines 8 and 9 a member variable of type Access::Tag. eg:
1 2 3 4 5 6 7 8 9 10
class Access
{
friendstruct Test;
struct Tag {};
};
struct Test
{
Access::Tag t;
};
@boolivar
look at comment at string 16 in original example - the question was not about friendship and access inside the class braces.
However, i found the answer in 11.4.2 of the standard, quote:
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.
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-speciļ¬ers and member declarations of the befriended class.
I guess every compiler we tried was right then, since there is no wrong answer. It's just
a matter of what version of the standard your compiler claims to support :)