4 ms·
static_cast from base to the wrong derived class is UB and can quickly become a vulnerability: https://wenke.gtisc.gatech.edu/papers/caver.pdf https://wenke.gt
by MauranKilom 7y ago
static_cast from base to the wrong derived class is UB and can quickly become a vulnerability:
https://wenke.gtisc.gatech.edu/papers/caver.pdf https://wenke.gtisc.gatech.edu/papers/caver.pdf
dynamic_cast to the wrong derived class has defined behavior and is imo worth relying on when speed is simply irrelevant (e.g. once per button click). That still leaves the argument open whether casting from base to derived is ever good, see other comment trees in this thread.
- keldaris 7y agoThis is probably a reasonable point in a subset (and mindset, frankly) of C++ that has very little overlap with the subset of C++ I use. Every C++ codebase I work on has RTTI (and exceptions) disabled immediately, so dynamic_cast isn't even an option. If it was, I would consider any use of dynamic_cast to be a bug because of the egregious performance implications. The correctness concern should be addressed either at compile time (either by construction, i.e. CRTP over runtime polymorphism or by other means) or by tests. Obviously, I'm extremely biased on this, because performance is effectively the primary (and often only) reason I use C++. In other domains, YMMV.