Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does RTTI seem frowned upon?

Tags:

c++

It seems everywhere I read that either a library boasts if not needing RTTI or an article advises against its use. What is so bad about it and why is it such a good thing not to need it?

Thanks

like image 468
jmasterx Avatar asked Dec 07 '22 13:12

jmasterx


2 Answers

  1. Because using it usually means you are subverting polymorphism (if (type is foo) { do this; } else if (type is bar) { do that; } else ...), which usually means you have engineered yourself into a corner and need to rethink your design.

  2. Because authors of C++ compilers put a lot of effort into optimising polymorphic behaviour, but less so into optimising use of RTTI.

like image 82
Oliver Charlesworth Avatar answered Dec 09 '22 02:12

Oliver Charlesworth


C++ allows a lot of static tricks with templates thus reducing the need of RTTI (everything is generic at the compile time, but concrete at the run-time).

On the contra, the "true" (SmallTalk-like) OOP way of dealing with classes requires dynamic binding and RTTI.

C++ allows both, but excessive dynamic_casts and virtual functions may and do degrade performance.

like image 40
Viktor Latypov Avatar answered Dec 09 '22 01:12

Viktor Latypov