Register Types - Hello World - 0.9.7 |RTTR
Register Types - Hello World

Let's start with the traditional hello world example.

#include <rttr/registration>
static void f() { std::cout << "Hello World" << std::endl; }
using namespace rttr;
{
using namespace rttr;
}
int main()
{
type::invoke("f", {});
}
// outputs: "Hello World"

When you need to reflect a property, or like in this case a free function, you need to include first #include <rttr/registration>. This will include everything necessary for creating the reflection information.

The macro RTTR_REGISTRATION must be placed outside of any function or class, just place directly into in your cpp file. This macro executes the register process before main is called, that has the advantage that this reflection information is directly available when main is called. When on the other hand the register calls are invoked manually, then these calls must be synchronized with the calls retrieving the type information. Otherwise invalid data will be returned. Remark that this macro can only be placed one-time in a source file, otherwise you will get an compile error.

The shortest way to invoke the function f() is to call type::invoke(). It requires the exact name of the free function and a vector of arguments. You can check whether the call was successful with checking the return value. When the returned variant is valid the call was successful, otherwise not.


previous