Note: Johannes pretty much nailed it. In '03 you cannot be friends with typedef - but if you know that you have a class, then you can refer to this injected class name .
Johannes's answer also has the advantage of using standard library functions, which is also always good.
#define some_compile_time_condition 0 class Foo; template <int Condition> class TestCondition { private: friend class Foo; struct Type { struct Bar; }; }; template <> class TestCondition<1> { public: typedef Bar Type; }; struct Bar { public: void foo (Foo &); }; class Foo { private: friend struct TestCondition< some_compile_time_condition >::Type::Bar; int m_i; }; void Bar::foo (Foo & foo) { foo.m_i = 0; }
It still differs from the requirement that Foo always has a friend, but make friends with class changes based on the value of the option.
An interesting question is the question of whether this is a violation of the ODR version of Foo with or without some_compile_time_condition set to 1.
source share