Consider we have some third party library with a class B derived from A using protected inheritance. So having an object of B we may not normally access its base A.
But we can create some additional class C (no objects of which will ever be constructed) deriving it from B and declare its friendship with the function that needs access to A-portion of B-objects. There are even two options how to get the access:
// some 3rd party library
struct A {};
struct B : protected A {};
// our code to get access to A
struct C : B { friend int main(); };
int main() {
const B & b = B{};
[[maybe_unused]] const A & a1 = static_cast<const C&>( b ); // #1: ok everywhere
[[maybe_unused]] const A & a2 = b; // #2: ok in GCC and MSVC
}
The option #1 with static_cast<const C&> works in all compilers. But is it well formed (no undefined behavior)?
The option #2 is even shorter, but it works only in GCC and MSVC, demo: https://gcc.godbolt.org/z/zj146bKsb
Given that Clang refuses to accept it, is it the only compiler that fulfills properly the standard here?