KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
dynamic_cast s are slower, but they are safer than static_cast s (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_cast s? 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.
Tags (comma-separated)
Save Edits
Cancel