dynamic_casts are slower, but they are safer than static_casts (when used with object hierarchies, of course). My question is, after I've ensured in my debug code that all (dynamic) casts are correct, is there any reason for me not to change them to static_casts?

I plan on doing this with the following construct. (Btw, can you think of a better name than assert_cast? Maybe debug_cast?)

#if defined(NDEBUG)

    template<typename T, typename U>
    T assert_cast(U other) {
        return static_cast<T>(other);
    }

#else

    template<typename T, typename U>
    T assert_cast(U other) {
        return dynamic_cast<T>(other);
    }

#endif

Edit: Boost already has something for this: polymorphic_downcast. Thanks to PlasmaHH for pointing that out.

Edit
Report