What do you think about the following guideline (which I think summarizes what has been said)? I found it easiest to represent it as a decision tree instead of as a paragraph.
Instead of having the decision for performance sensitive code we could also have a new method that does one or the other depending on whether one is in a debug build.
Mermaid Code for graph
flowchart
can_avoid_downcast["Is design without down-casting appropriate?\nE.g. using virtual methods."]
use_no_cast["Don't use explicit casting."]
is_type_check_necessary["Is a type check necessary?"]
use_dynamic_cast_ptr["Use dynamic_cast with a pointer type.\nAlways check the returned pointer."]
is_performance_sensitive["Is performance sensitive?"]
use_static_cast["Use static_cast for best performance.\nHard to find bug if assumption is wrong."]
use_dynamic_cast_ref["Use dynamic_cast with a reference type.\nThrows an exception if type is wrong."]
can_avoid_downcast --"yes"--> use_no_cast
can_avoid_downcast --"no"--> is_type_check_necessary
is_type_check_necessary --"yes"--> use_dynamic_cast_ptr
is_type_check_necessary --"no"--> is_performance_sensitive
is_performance_sensitive --"yes"--> use_static_cast
is_performance_sensitive --"no"--> use_dynamic_cast_ref