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?
Is it generally considered bad practice to use typeid in production code? Also, I
Share
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
typeidif 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 usingtypeidin favor of that better solution.It is often not a good idea to use
typeidbecause 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
typeidavoid returning astd::type_info, this shouldn’t cause any performance problems.typeidevaluates to aconst std::type_info&, so it doesn’t deep-copy any of the string information it contains. Most implementations have the actualstd::type_infoobject stored in the object’s virtual function table, so no copying is done internally.