Is it generally considered bad practice to use typeid
in production code? Also, I noticed typeid
returns type_info
, which includes some metadata (such as a string with the type's name); is there a way to deactivate this?
dynamic_cast
.It's hard to say whether the use of a particular language feature is "bad" or "good." It really depends on how you use it. There's nothing inherently wrong with using typeid
if it's the right tool for the job, but if there's a better solution to whatever problem you're solving, then you should avoid using typeid
in favor of that better solution.
It is often not a good idea to use typeid
because its use can often be avoided by using inheritance and virtual functions. If you can update your system in this way, then it might be a good idea to do so.
As for whether you can have typeid
avoid returning a std::type_info
, this shouldn't cause any performance problems. typeid
evaluates to a const std::type_info&
, so it doesn't deep-copy any of the string information it contains. Most implementations have the actual std::type_info
object stored in the object's virtual function table, so no copying is done internally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With