RTTR uses an own, in C++ implemented, alternative to the build in RTTI mechanism of C++. The reason for that are problems when using typeid
across shared boundaries, execution speed and sometimes not correct implemented. That means it is possible to disable RTTI completly in your target application and use RTTR instead.
The entry point for all type related operation is the class type. It is the central class for retrieving type objects or also querying all kind information about types.
A type object cannot be created directly. It is only possible to retrieve it with one of the three static getter member functions of the type class.
rttr::type::get<T>()
This function just expects one template argument. Use it to check against a known type.
Remark that when comparing types, internally always a simple integer comparison is done, instead how often with typeid, a string comparison.
rttr::type::get<T>(T&& obj)
This function takes a universal reference and returns from every given object the corresponding type object.
When this function is called for a glvalue expression whose type is a polymorphic class type, then the result refers to a type object representing the type of the most derived object.
If the type of the expression is a cv-qualified type, the result of the type::get() expression refers to a type object representing the cv-unqualified type.
Any top level
cv-qualifier of the given type T
will be removed.
rttr::type::get_by_name(string_view)
This function just expects the name of the type. This is useful when you know only the name of the type and cannot include the type itself into the source code.
- Remarks
- Before using the function type::get_by_name(), you have to use one time the function via type::get<T>(), otherwise the type is not registered in the type system.
Container Support
Unlike the type_info class, the rttr::type class will not need and additional wrapper to place type objects inside containers:
Summary
- type objects cannot be created, only retrieved via static member functions in the type class itself
- type objects are lightweight, they contain only a single integer ID
- every unique C++ type maps to the same unique ID, which will be used for comparing types
- type objects can be easily placed inside containers or maps
- retrieving a type object results always in the cv-unqualified type (
type::get(T)==type::get(const T))
; same liketypeid
) - retrieving a type object of a gvalue expression whose type is a polymorphic class, will refer to a type object representing the type of the most derived class