Default Arguments

When you want to register a method or a constructor which contains default arguments, you have to provide them RTTR explicitly. The reason for this is, default arguments are not part of the function signature. Nevertheless, RTTR provides a mechanism to register functions with default arguments.

Please take a look at following example:

using namespace rttr;
void my_function(int a, bool b, const std::string& text, const int* ptr);
{
registration::method("my_function", &my_function)
(
default_arguments(true, std::string("default text"), nullptr)
);
}
static bind< detail::meth, detail::invalid_type, F, detail::public_access > method(string_view name, F f)
Register a method to this class.
Definition access_levels.h:34
detail::default_args< TArgs... > default_arguments(TArgs &&...args)
The default_arguments function should be used add default arguments, for constructors or a methods du...
#define RTTR_REGISTRATION
Use this macro to automatically register your reflection information to RTTR before main is called.
Definition registration.h:745

The default arguments has to be provided via the function: default_arguments(). Place the call in the () operator of the returned bind object.

The function has following synopsis:

template<typename...TArgs>
detail::default_args<TArgs...> default_arguments(TArgs&&...args)

The values will be copied internally and will be provided during invoke of the method or constructor, when the corresponding argument is missing:

int main()
{
using namespace rttr;
method meth = type::get_global_method("my_function");
// the default values: 'true', 'std::string("default text"'), 'nullptr' will be forwarded by RTTR automatically
variant var = meth.invoke(instance(), 23);
std::cout << var.is_valid(); // prints 'true'
// the default value: 'nullptr' will be forwarded by RTTR automatically
var = meth.invoke(instance(), 23, true, std::string("text"));
std::cout << var.is_valid(); // prints 'true'
}
The instance class is used for forwarding the instance of an object to invoke a property or method.
Definition instance.h:48
The method class provides several meta information about a method and can be invoked.
Definition method.h:121
variant invoke(instance object) const
Invokes the method represented by the current instance object.
static method get_global_method(string_view name) noexcept
Returns a global method with the name name.
The variant class allows to store data of any type and convert between these types transparently.
Definition variant.h:198
bool is_valid() const
Returns true if this variant is valid, that means the variant is holding some data.
Remarks
The given arguments must match the signature from the starting position to the right most argument.

The following snippet will not compile and will raise a static_assert:

using namespace rttr;
void my_function(int a, bool b, const std::string& text, const int* ptr);
{
registration::method("my_function", &my_function)
(
default_arguments(std::string("default text")) // default value for "ptr" not given
);
}

Every method and constructor can have default arguments.