The following code is rejected by GCC/Clang/MSVC though it seems to be able to compile.
class B {
};
class C : private B {
friend B& to_B(C& c) {
return static_cast<B&>(c);
}
};
class D : private C {
friend C& to_C(D& d) {
return static_cast<C&>(d);
}
friend B& to_B(D& d) {
return to_B(to_C(d));
}
};
Why the compilation fails?
GCC error message:
<source>:12:12: error: 'class B B::B' is private within this context
12 | friend B& to_B(D& d) {
| ^
<source>:3:7: note: declared private here
3 | class C : private B {
| ^