Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Comparison of raw pointer properties does not work #55

Closed
rovarma opened this issue Apr 26, 2017 · 0 comments
Closed

Comparison of raw pointer properties does not work #55

rovarma opened this issue Apr 26, 2017 · 0 comments
Labels

Comments

@rovarma
Copy link

rovarma commented Apr 26, 2017

When comparing two pointer properties, the wrong result will always be returned. To clarify, assuming some two objects with a raw pointer property:

RTTI::Variant valueA = property.get_value(objectA);
RTTI::Variant valueB = property.get_value(objectB);

bool equal = (valueA == valueB); // Always false

The reason for this is that the following specialization of compare_equal is always selected in the case of raw pointer properties:

template<typename T>
RTTR_INLINE typename std::enable_if<!is_comparable_type<T>::value && !std::is_array<T>::value, bool>::type
compare_equal(const T& lhs, const T& rhs)
{
    return compare_types_equal(&lhs, &rhs, type::get<T>());
}

This is the wrong specialization, because it results in comparing the addresses of two pointers, instead of the values of the pointers.

It should select the following specialization instead:

template<typename T>
RTTR_INLINE typename std::enable_if<is_comparable_type<T>::value && !std::is_array<T>::value, bool>::type
compare_equal(const T& lhs, const T& rhs)
{
    return (lhs == rhs);
}

This is the correct behaviour. The fix is simple: raw pointers should be included in the list of comparable types, that is, is_comparable_type<T> should return true when T = T*.

Can be fixed by simply adding std::is_pointer<T>::value to the definition of is_comparable_type, ie:

template<typename T>
using is_comparable_type = std::integral_constant<bool, std::is_same<T, std::string>::value ||
                                                        std::is_same<T, string_view>::value ||
                                                        std::is_arithmetic<T>::value ||
                                                        std::is_enum<T>::value ||
                                                        std::is_same<T, std::nullptr_t>::value ||
							std::is_pointer<T>::value
                                                 >;

Note: this problem also affects the less-than comparison, but the fix to is_comparable_type also fixes that one.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants