Run-time Type Information: An Easy Solution
Run-time type information (RTTI) is an useful feature for many C++ applications. Essentially, RTTI provides some dynamic reflection capabilities to an otherwise statically compiled C++ program, such as the ability to
- ask the type of an object
- compare two types, e.g. find out if they are equal or related by inheritance
- create an object of a type which is supplied as a string
Many modern compilers, such as Visual C++, Borland C++, and gcc, provide built-in RTTI functions. These are also part of Standard C++. However, in certain contexts, developers need a stand-alone RTTI facility. Examples are embedded software, where one does not want to include the full-blown overhead of the compiler's built-in RTTI mechanism; situations when one wants to custom control the way RTTI acts; and situations where we want to extend the standard RTTI with additional capabilities, such as creating an object from a type given as a string.
The RTTI Library
We have developed a RTTI library which provides all the above capabilities. The library is extremely small and has a minimal memory and execution footprint. It can be used with any C++ compiler, including outdated/non-standard ones, as it uses only basic facilities (virtual functions and the !!!string.h!!! standard header). The library works also when classes are compiled in multiple static or dynamic libraries.
Download the code
The code of the RTTI library is available here.
A detailed description of the RTTI library design is available here.
The RTTI library has been used in tens of industrial, open-source, and research projects (of which I do not maintain a full list). Since the code is distributed under the MIT license, feel free to use it as you wish.