Some companies and libraries have specific requirements for their C++ code, such as successful compilation without RTTI.
In this small recipe, we'll not just detect disabled RTTI, but also write a Boost like library from scratch that stores information about types, and compares types at runtime, even without typeid
.
Detecting disabled RTTI, storing information about types, and comparing types at runtime are tricks that are widely used across Boost libraries.
- To do this, we first need to include the following header:
#include <boost/config.hpp>
- Let's first look at the situation where RTTI is enabled and the C++11
std::type_index
class is available:
#if !defined(BOOST_NO_RTTI) \ && !defined(BOOST_NO_CXX11_HDR_TYPEINDEX) #include <typeindex> using std::type_index; template <class T> type_index type_id() { return typeid(T); }
- Otherwise...