The variant class allows to store data of any type and convert between these types transparently. More...
#include <variant.h>
Public Member Functions | |
variant () | |
Constructs an invalid variant. | |
variant (const variant &other) | |
Constructs a new variant object from the given variant other . | |
template<typename T, typename Tp = detail::decay_variant_t<T>> | |
variant (T &&val) | |
Constructs a new variant with the new value val . | |
variant (variant &&other) | |
Constructs a new variant via move constructor. | |
~variant () | |
Destroys the variant and the contained object. | |
template<typename T> | |
bool | can_convert () const |
Returns true if the contained value can be converted to the given type T . | |
bool | can_convert (const type &target_type) const |
Returns true if the contained value can be converted to the given type target_type ; otherwise false. | |
void | clear () |
When the variant contains a value, then this function will clear the content. | |
template<typename T> | |
T | convert (bool *ok=nullptr) const |
Converts the containing data to a new value of type T and return this value. | |
bool | convert (const type &target_type) |
Converts the containing variant internally to the given type target_type . | |
template<typename T> | |
bool | convert (T &value) const |
Converts the containing data to the given value value and returns a bool flag that indicated whether the conversion was successful or not. | |
variant_associative_view | create_associative_view () const |
Creates a variant_associative_view from the containing value, when the type or its raw type or the wrapped type is an associative container. | |
variant_sequential_view | create_sequential_view () const |
Creates a variant_sequential_view from the containing value, when the type or its raw type or the wrapped type is an sequential container. | |
variant | extract_wrapped_value () const |
Extracts the wrapped value and copies its content into a new variant. | |
type | get_type () const |
Returns the type object of underlying data. | |
template<typename T> | |
T & | get_value () |
Returns a reference to the containing value as type T . | |
template<typename T> | |
const T & | get_value () const |
Returns a reference to the containing value as type T . | |
template<typename T> | |
const T & | get_wrapped_value () const |
Returns a reference to the contained wrapped value as type T . | |
bool | is_associative_container () const |
Returns true, when for the underlying or the wrapped type an associative_mapper exists. | |
bool | is_sequential_container () const |
Returns true, when for the underlying or the wrapped type an sequential_mapper exists. | |
template<typename T> | |
bool | is_type () const |
Returns true if the containing variant data is of the given template type T. | |
bool | is_valid () const |
Returns true if this variant is valid, that means the variant is holding some data. | |
operator bool () const | |
Convenience function to check if this variant is valid or not. | |
bool | operator!= (const variant &other) const |
Compares this variant with other and returns true if they are not equal; otherwise returns false. | |
bool | operator< (const variant &other) const |
Compares this variant with other and returns true if this is less than other , otherwise returns false. | |
bool | operator<= (const variant &other) const |
Compares this variant with other and returns true if this is less or equal than other , otherwise returns false. | |
variant & | operator= (const variant &other) |
Assigns the value of the other variant to this variant. | |
template<typename T, typename Tp = detail::decay_variant_t<T>> | |
variant & | operator= (T &&other) |
Assigns the value of the other object to this variant. | |
variant & | operator= (variant &&other) |
Assigns the value of the other variant to this variant. | |
bool | operator== (const variant &other) const |
Compares this variant with other and returns true if they are equal; otherwise returns false. | |
bool | operator> (const variant &other) const |
Compares this variant with other and returns true if this is greater than other , otherwise returns false. | |
bool | operator>= (const variant &other) const |
Compares this variant with other and returns true if this is greater or equal then other , otherwise returns false. | |
void | swap (variant &other) |
Swaps the content of this variant with other variant. | |
bool | to_bool () const |
Returns the variant as a bool if this variant is of type bool. | |
double | to_double (bool *ok=nullptr) const |
Returns the containing variant as a double when the type is a double. | |
float | to_float (bool *ok=nullptr) const |
Returns the containing variant as a float when the type is a float. | |
int | to_int (bool *ok=nullptr) const |
Returns the containing variant as an int when the type is an integer. | |
int16_t | to_int16 (bool *ok=nullptr) const |
Returns the containing variant as an int16_t when the type is an int16_t. | |
int32_t | to_int32 (bool *ok=nullptr) const |
Returns the containing variant as an int32_t when the type is an int32_t. | |
int64_t | to_int64 (bool *ok=nullptr) const |
Returns the containing variant as an int64_t when the type is an int64_t. | |
int8_t | to_int8 (bool *ok=nullptr) const |
Returns the containing variant as an int8_t when the type is an int8_t. | |
std::string | to_string (bool *ok=nullptr) const |
Returns the containing variant as a std::string when the type is a std::string. | |
uint16_t | to_uint16 (bool *ok=nullptr) const |
Returns the containing variant as an uint16_t when the type is an uint16_t. | |
uint32_t | to_uint32 (bool *ok=nullptr) const |
Returns the containing variant as an uint32_t when the type is an uint32_t. | |
uint64_t | to_uint64 (bool *ok=nullptr) const |
Returns the containing variant as an uint64_t when the type is an uint64_t. | |
uint8_t | to_uint8 (bool *ok=nullptr) const |
Returns the containing variant as an uint8_t when the type is an uint8_t. |
Detailed Description
The variant class allows to store data of any type and convert between these types transparently.
This class serves as container for any given single type. It can hold one value at a time (using containers you can hold multiple types e.g. std::vector<int>). Remark that the content is copied into the variant class. Even raw arrays (e.g. int[10]) are copied. However, the internal implementation of variant has an optimization for storing small types, which avoid heap allocation.
The main purpose of this class is to be the return value for property and method invokes or as container for storing meta data.
Copying and Assignment
A variant object can be copied and assigned, however each copy will perform a copy of the contained value.
Typical Usage
Extract Value
For extracting a value out of a variant you can use the get_value() function. This will return a const reference to the contained value. However, you must instantiated this function with the exact type of the stored value, otherwise undefined behaviour will occur. Therefore you should check it's type before extracting with is_type<T>().
See following example:
Conversion
The variant class offers three possibilities to convert to a new type.
- convert(const type& target_type) - convert the variant internally to a new type
- convert<T>(bool *ok) - convert the contained value to an internally default created value of type
T
and returns this new value - convert<T>(T& value) - convert the contained value to a given
value
of typeT
See following example code:
- Remarks
- It is possible that can_convert() will return true, but convert() will actually fail and return false. The reason for this is can_convert() will return the general ability of converting between types given suitable data; when no suitable data is given, the conversion cannot be performed.
A good example is the conversion from std::string to int.
Hence, it is important to have both functions return true for a successful conversion.
Custom Converter
The variant class allows to convert from and to user-defined types, therefore you have to register a conversion function.
See following example code:
For more information see type::register_converter_func()
- See also
- variant_array_view
Constructor & Destructor Documentation
◆ variant() [1/4]
rttr::variant::variant | ( | ) |
◆ variant() [2/4]
rttr::variant::variant | ( | T && | val | ) |
Constructs a new variant with the new value val
.
The value will be copied or moved into the variant.
◆ variant() [3/4]
rttr::variant::variant | ( | const variant & | other | ) |
Constructs a new variant object from the given variant other
.
◆ variant() [4/4]
rttr::variant::variant | ( | variant && | other | ) |
Constructs a new variant via move constructor.
◆ ~variant()
rttr::variant::~variant | ( | ) |
Destroys the variant and the contained object.
Member Function Documentation
◆ can_convert() [1/2]
bool rttr::variant::can_convert | ( | ) | const |
Returns true if the contained value can be converted to the given type T
.
Otherwise false.
- Returns
- True if this variant can be converted to T; otherwise false.
- See also
- convert(), type::register_converter_func()
◆ can_convert() [2/2]
bool rttr::variant::can_convert | ( | const type & | target_type | ) | const |
Returns true if the contained value can be converted to the given type target_type
; otherwise false.
The returned value indicates that a conversion is in general possible. However a conversion might still fail when doing the actual conversion with convert(). An example is the conversion from a string to a number. When the string does not contain non-numeric characters, the conversion will not succeed.
- Returns
- True if this variant can be converted to
target_type
; otherwise false.
- See also
- convert(), type::register_converter_func()
◆ clear()
void rttr::variant::clear | ( | ) |
When the variant contains a value, then this function will clear the content.
- Remarks
- After calling this function is_valid() will return false.
◆ convert() [1/3]
T rttr::variant::convert | ( | bool * | ok = nullptr | ) | const |
Converts the containing data to a new value of type T
and return this value.
If ok
is non-null: *ok
is set to true when the value was successfully converted to T
; otherwise *ok
is set to false. The type T
must meet the requirement to be default constructible.
In order to enable a custom type conversion, a conversion function has to be registered via type::register_converter_func().
- Remarks
- Before doing the conversion you should check whether it is in general possible to convert to type
T
with the function can_convert(). When the conversion fails, a default constructed value of typeT
is returned.
- Returns
- The converted value as type
T
.
◆ convert() [2/3]
bool rttr::variant::convert | ( | const type & | target_type | ) |
Converts the containing variant internally to the given type target_type
.
When the conversion was successfully the function will return true. When the conversion fails, then the containing variant value stays the same and the function will return false.
There are already certain standard type conversions implemented:
- Conversion of all arithmetic types (e.g. double to int, 'std::size_t' to 'int8_t' and so on)
- Conversion of all arithmetic types to and from std::string.
- Conversion of enum types to std::string, its underlying arithmetic types and vice versa.
- Conversion of wrapper classes to wrapped types and vice versa (e.g, std::shared_ptr<int> to int*)
- Conversion of raw pointers to its derived types, if a rttr_cast to the type described by
target_type
would succeed.
See therefore following example code:
Additionally, it is possible to add custom conversion function, which has to be registered via type::register_converter_func().
- Returns
- True if this variant can be converted to
target_type
; otherwise false.
◆ convert() [3/3]
bool rttr::variant::convert | ( | T & | value | ) | const |
Converts the containing data to the given value value
and returns a bool flag that indicated whether the conversion was successful or not.
When you need to convert to a type which cannot be default constructed use this function.
See following example code:
In order to enable a custom type conversion, a conversion function has to be registered via type::register_converter_func().
- Remarks
- Before doing the conversion you should check whether it is in general possible to convert to type
T
with the function can_convert()
- Returns
- True if the contained data could be converted to
value
; otherwise false.
◆ create_associative_view()
variant_associative_view rttr::variant::create_associative_view | ( | ) | const |
Creates a variant_associative_view from the containing value, when the type or its raw type or the wrapped type is an associative container.
Otherwise a default constructed variant_associative_view will be returned.
A typical example is the following:
- Returns
- A variant_associative_view object.
- See also
- can_convert(), convert()
◆ create_sequential_view()
variant_sequential_view rttr::variant::create_sequential_view | ( | ) | const |
Creates a variant_sequential_view from the containing value, when the type or its raw type or the wrapped type is an sequential container.
Otherwise a default constructed variant_sequential_view will be returned.
A typical example is the following:
- Returns
- A variant_sequential_view object.
- See also
- can_convert(), convert()
◆ extract_wrapped_value()
variant rttr::variant::extract_wrapped_value | ( | ) | const |
Extracts the wrapped value and copies its content into a new variant.
- Remarks
- Calling this method works only for wrapped types which are copiable. When you work with custom types, which are not copyable, the variant will be invalid
- Returns
- A variant with the wrapped value.
- See also
- type::is_wrapper()
◆ get_type()
type rttr::variant::get_type | ( | ) | const |
◆ get_value() [1/2]
T & rttr::variant::get_value | ( | ) |
Returns a reference to the containing value as type T
.
- Remarks
- Only call this method when it is possible to return the containing value as the given type
T
. Use therefore the method is_type(). Otherwise the call leads to undefined behaviour. Also make sure you don't clean this variant, when you still hold a reference to the containing value.
- See also
- is_type(), variant_cast<T>
- Returns
- A reference to the stored value.
◆ get_value() [2/2]
const T & rttr::variant::get_value | ( | ) | const |
Returns a reference to the containing value as type T
.
- Remarks
- Only call this method when it is possible to return the containing value as the given type
T
. Use therefore the method is_type(). Otherwise the call leads to undefined behaviour. Also make sure you don't clean this variant, when you still hold a reference to the containing value.
- See also
- is_type(), variant_cast<T>
- Returns
- A reference to the stored value.
◆ get_wrapped_value()
const T & rttr::variant::get_wrapped_value | ( | ) | const |
Returns a reference to the contained wrapped value as type T
.
- Remarks
- Only call this method when it is possible to return the containing value as the given type
T
. Use therefore the method get_wrapped_type(). Otherwise the call leads to undefined behaviour.
- See also
- rttr::type::get_wrapped_type()
- Returns
- A reference to the stored wrapped value.
◆ is_associative_container()
bool rttr::variant::is_associative_container | ( | ) | const |
Returns true, when for the underlying or the wrapped type an associative_mapper exists.
- Returns
- True if the containing value is an associative container; otherwise false.
◆ is_sequential_container()
bool rttr::variant::is_sequential_container | ( | ) | const |
Returns true, when for the underlying or the wrapped type an sequential_mapper exists.
- Returns
- True if the containing value is an sequentail container; otherwise false.
◆ is_type()
bool rttr::variant::is_type | ( | ) | const |
Returns true if the containing variant data is of the given template type T.
- Returns
- True if variant data is of type T, otherwise false.
◆ is_valid()
bool rttr::variant::is_valid | ( | ) | const |
Returns true if this variant is valid, that means the variant is holding some data.
When the variant doesn't hold any data it will return false.
- Remarks
- A variant can also hold void data, this is used to indicate that a method call, which has no return value, was successfully. In this case, there is no data actually stored, but this function will return true.
- Returns
- True if this variant is valid, otherwise false.
◆ operator bool()
|
explicit |
Convenience function to check if this variant is valid or not.
- See also
- is_valid()
- Returns
- True if this variant is valid, otherwise false.
◆ operator!=()
bool rttr::variant::operator!= | ( | const variant & | other | ) | const |
Compares this variant with other
and returns true if they are not equal; otherwise returns false.
- Remarks
- In order to use this function with template types, like std::tuple<int, std::string>, you need to register the comparison operator to the type system with type::register_comparators<T>(). The reason for that is, template types might define the != operator, but not the contained template type.
- Note
- Comparability might not be available for the type stored in this variant or in
other
.
- See also
- operator==
- Returns
- A boolean with value true, that indicates both variant's are not equal, otherwise false.
◆ operator<()
bool rttr::variant::operator< | ( | const variant & | other | ) | const |
Compares this variant with other
and returns true if this is less than other
, otherwise returns false.
The variant uses the less than operator of the containing type. When other
is not of the same type as the containing type, it will try to convert to it and do then the less than check.
- Remarks
- In order to use this function with template types, like std::tuple<int, std::string>, you need to register the comparison operator to the type system with type::register_comparators<T>(). The reason for that is, template types might define the < operator, but not the contained template type.
- Note
- Comparability might not be available for the type stored in this variant or in
other
.
- See also
- operator>
- Returns
- A boolean with value true, that indicates that this variant is less than
other
, otherwise false.
◆ operator<=()
bool rttr::variant::operator<= | ( | const variant & | other | ) | const |
Compares this variant with other
and returns true if this is less or equal than other
, otherwise returns false.
The variant uses the less than and equality operator of the containing type to get the result of the comparision.
- Note
- Comparability might not be available for the type stored in this variant or in
other
.
- See also
- operator<, operator==
- Returns
- A boolean with value true, that indicates that this variant is less or equal than
other
, otherwise false.
◆ operator=() [1/3]
Assigns the value of the other variant to this variant.
- Returns
- A reference to the variant with the new data.
◆ operator=() [2/3]
variant & rttr::variant::operator= | ( | T && | other | ) |
Assigns the value of the other
object to this variant.
- Returns
- A reference to the variant with the new data.
◆ operator=() [3/3]
Assigns the value of the other variant to this variant.
- Returns
- A reference to the variant with the new data.
◆ operator==()
bool rttr::variant::operator== | ( | const variant & | other | ) | const |
Compares this variant with other
and returns true if they are equal; otherwise returns false.
The variant uses the equality operator of the containing type to check for equality. When other
is not of the same type as the containing type, it will try to convert to it and do then the equality check.
- Remarks
- In order to use this function with template types, like std::tuple<int, std::string>, you need to register the comparison operator to the type system with type::register_comparators<T>(). The reason for that is, template types might define the == operator, but not the contained template type.
- Note
- Comparability might not be available for the type stored in this variant or in
other
.
- See also
- operator!=
- Returns
- A boolean with value true, that indicates both variant's are equal, otherwise false.
◆ operator>()
bool rttr::variant::operator> | ( | const variant & | other | ) | const |
Compares this variant with other
and returns true if this is greater than other
, otherwise returns false.
The variant uses the less than and equality operator of the containing type to get the result of the comparision.
- Note
- Comparability might not be available for the type stored in this variant or in
other
.
- See also
- operator<
- Returns
- A boolean with value true, that indicates that this variant is greater than
other
, otherwise false.
◆ operator>=()
bool rttr::variant::operator>= | ( | const variant & | other | ) | const |
Compares this variant with other
and returns true if this is greater or equal then other
, otherwise returns false.
The variant uses the greater than and equality operator of the containing type to get the result of the comparision.
- Note
- Comparability might not be available for the type stored in this variant or in
other
.
- See also
- operator>, operator==
- Returns
- A boolean with value true, that indicates that this variant is greater or equal than
other
, otherwise false.
◆ swap()
void rttr::variant::swap | ( | variant & | other | ) |
Swaps the content of this variant with other
variant.
◆ to_bool()
bool rttr::variant::to_bool | ( | ) | const |
Returns the variant as a bool if this variant is of type bool.
Returns true if the variant contains an arithmetic type which value is non-zero or if the variant contains a std::string and its lower-case content is not one of the following: "" (empty), "0" or "false"; otherwise returns false.
Also any user-defined conversion function from the source type to bool will be executed when necessary.
- See also
- can_convert(), is_type()
- Returns
- A bool value.
◆ to_double()
double rttr::variant::to_double | ( | bool * | ok = nullptr | ) | const |
Returns the containing variant as a double when the type is a double.
When the variant contains an arithmetic type or an std::string then a conversion to double will be tried. Also any user-defined conversion function from the source type to double will be executed when necessary.
If ok
is non-null: *ok
is set to true if the value could be converted to an double; otherwise *ok
is set to false.
- Remarks
- A value overflow is not allowed, so if the internal value is larger than double, the conversion will fail. Precision loss, such as in conversion from double to float on platforms where they differ in size is allowed. A conversion from std::string which contains non-numeric characters will fail.
- See also
- can_convert(), is_type()
- Returns
- A double value.
◆ to_float()
float rttr::variant::to_float | ( | bool * | ok = nullptr | ) | const |
Returns the containing variant as a float when the type is a float.
When the variant contains an arithmetic type or an std::string then a conversion to float will be tried. Also any user-defined conversion function from the source type to float will be executed when necessary.
If ok
is non-null: *ok
is set to true if the value could be converted to an float; otherwise *ok
is set to false.
- Remarks
- A value overflow is not allowed, so if the internal value is larger than float, the conversion will fail. Precision loss, such as in conversion from double to float on platforms where they differ in size is allowed. A conversion from std::string which contains non-numeric characters will fail.
- See also
- can_convert(), is_type()
- Returns
- A float value.
◆ to_int()
int rttr::variant::to_int | ( | bool * | ok = nullptr | ) | const |
Returns the containing variant as an int when the type is an integer.
When the variant contains an arithmetic type or an std::string then a conversion to int will be tried. Also any user-defined conversion function from the source type to int will be executed when necessary.
If ok
is non-null: *ok
is set to true if the value could be converted to an int; otherwise *ok
is set to false.
- Remarks
- A value overflow is not allowed, so if the internal value is larger than int, the conversion will fail. Precision loss, such as in conversion from floating-point to int on platforms where they differ in size is allowed. A conversion from std::string which contains non-numeric characters will fail.
- See also
- can_convert(), is_type()
- Returns
- An int value.
◆ to_int16()
int16_t rttr::variant::to_int16 | ( | bool * | ok = nullptr | ) | const |
Returns the containing variant as an int16_t when the type is an int16_t.
When the variant contains an arithmetic type or an std::string then a conversion to int16_t will be tried. Also any user-defined conversion function from the source type to int16_t will be executed when necessary.
If ok
is non-null: *ok
is set to true if the value could be converted to an int16_t; otherwise *ok
is set to false.
- Remarks
- A value overflow is not allowed, so if the internal value is larger than int16_t, the conversion will fail. Precision loss, such as in conversion from floating-point to int16_t on platforms where they differ in size is allowed. A conversion from std::string which contains non-numeric characters will fail.
- See also
- can_convert(), is_type()
- Returns
- A int16_t value.
◆ to_int32()
int32_t rttr::variant::to_int32 | ( | bool * | ok = nullptr | ) | const |
Returns the containing variant as an int32_t when the type is an int32_t.
When the variant contains an arithmetic type or an std::string then a conversion to int32_t will be tried. Also any user-defined conversion function from the source type to int32_t will be executed when necessary.
If ok
is non-null: *ok
is set to true if the value could be converted to an int32_t; otherwise *ok
is set to false.
- Remarks
- A value overflow is not allowed, so if the internal value is larger than int32_t, the conversion will fail. Precision loss, such as in conversion from floating-point to int32_t on platforms where they differ in size is allowed. A conversion from std::string which contains non-numeric characters will fail.
- See also
- can_convert(), is_type()
- Returns
- A int32_t value.
◆ to_int64()
int64_t rttr::variant::to_int64 | ( | bool * | ok = nullptr | ) | const |
Returns the containing variant as an int64_t when the type is an int64_t.
When the variant contains an arithmetic type or an std::string then a conversion to int64_t will be tried. Also any user-defined conversion function from the source type to int64_t will be executed when necessary.
If ok
is non-null: *ok
is set to true if the value could be converted to an int64_t; otherwise *ok
is set to false.
- Remarks
- A value overflow is not allowed, so if the internal value is larger than int64_t, the conversion will fail. Precision loss, such as in conversion from floating-point to int64_t on platforms where they differ in size is allowed. A conversion from std::string which contains non-numeric characters will fail.
- See also
- can_convert(), is_type()
- Returns
- A int64_t value.
◆ to_int8()
int8_t rttr::variant::to_int8 | ( | bool * | ok = nullptr | ) | const |
Returns the containing variant as an int8_t when the type is an int8_t.
When the variant contains an arithmetic type or an std::string then a conversion to int8_t will be tried. Also any user-defined conversion function from the source type to int8_t will be executed when necessary.
If ok
is non-null: *ok
is set to true if the value could be converted to an int8_t; otherwise *ok
is set to false.
- Remarks
- A value overflow is not allowed, so if the internal value is larger than int8_t, the conversion will fail. Precision loss, such as in conversion from floating-point to int8_t on platforms where they differ in size is allowed. A conversion from std::string which contains non-numeric characters will fail.
- See also
- can_convert(), is_type()
- Returns
- A int8_t value.
◆ to_string()
std::string rttr::variant::to_string | ( | bool * | ok = nullptr | ) | const |
Returns the containing variant as a std::string when the type is a std::string.
When the variant contains an arithmetic type then a conversion to std::string will be done. Also any user-defined conversion function from the source type to std::string will be executed when necessary.
If ok
is non-null: *ok
is set to true if the value could be converted to an std::string; otherwise *ok
is set to false.
- See also
- can_convert(), is_type()
- Returns
- A std::string value.
◆ to_uint16()
uint16_t rttr::variant::to_uint16 | ( | bool * | ok = nullptr | ) | const |
Returns the containing variant as an uint16_t when the type is an uint16_t.
When the variant contains an arithmetic type or an std::string then a conversion to uint16_t will be tried. Also any user-defined conversion function from the source type to uint16_t will be executed when necessary.
If ok
is non-null: *ok
is set to true if the value could be converted to an uint16_t; otherwise *ok
is set to false.
- Remarks
- A value overflow is not allowed, so if the internal value is larger than uint16_t, the conversion will fail. Also a loss of signedness is not allowed, that means a negative signed integer cannot be converted to uint16_t. Precision loss, such as in conversion from floating-point to uint16_t on platforms where they differ in size is allowed. A conversion from std::string which contains non-numeric characters will fail.
- See also
- can_convert(), is_type()
- Returns
- A uint16_t value.
◆ to_uint32()
uint32_t rttr::variant::to_uint32 | ( | bool * | ok = nullptr | ) | const |
Returns the containing variant as an uint32_t when the type is an uint32_t.
When the variant contains an arithmetic type or an std::string then a conversion to uint32_t will be tried. Also any user-defined conversion function from the source type to uint32_t will be executed when necessary.
If ok
is non-null: *ok
is set to true if the value could be converted to an uint32_t; otherwise *ok
is set to false.
- Remarks
- A value overflow is not allowed, so if the internal value is larger than uint32_t, the conversion will fail. Also a loss of signedness is not allowed, that means a negative signed integer cannot be converted to uint32_t. Precision loss, such as in conversion from floating-point to uint32_t on platforms where they differ in size is allowed. A conversion from std::string which contains non-numeric characters will fail.
- See also
- can_convert(), is_type()
- Returns
- A uint32_t value.
◆ to_uint64()
uint64_t rttr::variant::to_uint64 | ( | bool * | ok = nullptr | ) | const |
Returns the containing variant as an uint64_t when the type is an uint64_t.
When the variant contains an arithmetic type or an std::string then a conversion to uint64_t will be tried. Also any user-defined conversion function from the source type to uint64_t will be executed when necessary.
If ok
is non-null: *ok
is set to true if the value could be converted to an uint8_t; otherwise *ok
is set to false.
- Remarks
- A value overflow is not allowed, so if the internal value is larger than uint64_t, the conversion will fail. Also a loss of signedness is not allowed, that means a negative signed integer cannot be converted to uint64_t. Precision loss, such as in conversion from floating-point to uint64_t on platforms where they differ in size is allowed. A conversion from std::string which contains non-numeric characters will fail.
- See also
- can_convert(), is_type()
- Returns
- A uint64_t value.
◆ to_uint8()
uint8_t rttr::variant::to_uint8 | ( | bool * | ok = nullptr | ) | const |
Returns the containing variant as an uint8_t when the type is an uint8_t.
When the variant contains an arithmetic type or an std::string then a conversion to uint8_t will be tried. Also any user-defined conversion function from the source type to uint8_t will be executed when necessary.
If ok
is non-null: *ok
is set to true if the value could be converted to an uint8_t; otherwise *ok
is set to false.
- Remarks
- A value overflow is not allowed, so if the internal value is larger than uint8_t, the conversion will fail. Also a loss of signedness is not allowed, that means a negative signed integer cannot be converted to uint8_t. Precision loss, such as in conversion from floating-point to uint8_t on platforms where they differ in size is allowed. A conversion from std::string which contains non-numeric characters will fail.
- See also
- can_convert(), is_type()
- Returns
- A uint8_t value.
The documentation for this class was generated from the following file:
Generated on Fri Feb 23 2024 11:50:43 for rttr - 0.9.6 by doxygen.