Retrieving rttr::type objects - 0.9.6 |RTTR
Retrieving rttr::type objects

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.

#include <rttr/type> // when working with type objects, this is the only header you have to include
//...
using namespace rttr;
type my_int_type = type::get<int>(); // statically or
type my_bool_type = type::get(true); // dynamically

rttr::type::get<T>()

This function just expects one template argument. Use it to check against a known type.

type::get<int>() == type::get<int>(); // yields to true
type::get<int>() == type::get<bool>(); // yields to false

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.

int int_obj = 23;
int* int_obj_ptr = &int_obj;
const int* c_int_obj_ptr = int_obj_ptr;
type::get<int>() == type::get(int_obj); // yields to true
type::get<int*>() == type::get(int_obj_ptr); // yields to true
type::get<const int*>() == type::get(c_int_obj_ptr); // yields to true

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.

struct Base {};
struct Derived : Base {};
Derived d;
Base& base = d;
type::get<Derived>() == type::get(base) // yields to true
type::get<Base>() == type::get(base) // yields to false
// REMARK when called with pointers:
Base* base_ptr = &d;
type::get<Derived>() == type::get(base_ptr); // yields to false
type::get<Base*>() == type::get(base_ptr); // yields to true

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.

class D { ... };
D d1;
const D d2;
type::get(d1) == type::get(d2); // yields true
type::get<D>() == type::get<const D>(); // yields true
type::get<D>() == type::get(d2); // yields true
type::get<D>() == type::get<const D&>(); // yields true
type::get<D>() == type::get<const D*>(); // yields false

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.

type::get_by_name("int") == type::get<int>(); // yields to true
type::get_by_name("bool") == type::get<int>(); // yields to false
type::get_by_name("MyNameSpace::MyStruct") == type::get<MyNameSpace::MyStruct>(); // yields to true
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:

std::vector<rttr::type> type_list;
std::map<rttr::type, std::string> mapping;
std::unordered_map<rttr::type, std::string> type_names;

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 like typeid)
  • 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

previous