The last line of this code fails to compile with castingAndTernary.cpp:15: error: conditional expression between distinct pointer types ‘D1*’ and ‘D2*’ lacks a cast
A really smart compiler could have no difficulty because both can be safely casted to
B*
Irrelevant. The standard mandates this behaviour. A really smart compiler behaves as observed.
The use of your custom cast is actually fine (and your reluctance for using an explicit cast is well-placed). However, I’d use a different name: upcast
– since that’s happening here: a cast upwards in the inheritance hierarchy.