class private_object
{
private:
struct make_public;
friend struct make_public;
static void method1() {}
};
struct private_object::make_public
{
class nested_outer
{
void callFromOuter()
{ private_object::method1(); } // Should this be an error?
class nested_inner
{
void callFromInner()
{ private_object::method1(); } // How about this one?
};
};
};
This friendship issue came up when I was trying to port an open source project to compile under borland. According to parashift and two semi-related questions here and here, the above example should not be valid.
However, after testing it on seven different compilers1, only borland and dmc complained. This behavior surprised me because I wasn't expecting friendship to be transitive in nested classes.
So this raises a couple of questions:
- What is the right behavior? I'm guessing it's the one accepted by most compilers.
- If this is the correct behavior, why is this instance of friendship transitivity ok?
- If this is correct then that would also imply a change in the standard. What might be the reasons for allowing this in the standard?
- For compilers that rejected this code, what would be an appropriate workaround? Keep in mind that an actual project might contain fairly deep nesting so I'm looking for a solution that's semi-scalable.
1. tested on mingw-gcc 4.5.2, clang, borland c++ builder2007, digital mars, open watcom, visualc2010 and comeau online