Skip to content

Commit

Permalink
Turns out libraries override operator== to not return a bool values (…
Browse files Browse the repository at this point in the history
…see OpenCV Cv::Mat==). So update the is_comparable to check for this.
  • Loading branch information
cfis committed Nov 17, 2024
1 parent f44740d commit 326de94
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
5 changes: 4 additions & 1 deletion rice/traits/rice_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ namespace Rice
struct is_comparable : std::false_type {};

template<typename T>
struct is_comparable<T, std::void_t<decltype(std::declval<T>() == std::declval<T>())>> : std::true_type {};
struct is_comparable<T, std::void_t<
// Does the class implement operator== and does it return a boolean value?
decltype(std::declval<T>() == std::declval<T>() && true)
>> : std::true_type {};

template<typename T>
constexpr bool is_comparable_v = is_comparable<T>::value;
Expand Down
43 changes: 43 additions & 0 deletions test/test_Stl_Vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,49 @@ TESTCASE(Comparable)
ASSERT_EQUAL(1, detail::From_Ruby<size_t>().convert(result.value()));
}

namespace
{
class ComparableButNotBool
{
public:
ComparableButNotBool(uint32_t value) : value_(value)
{
};

std::string operator==(const ComparableButNotBool& other)
{
return "not a boolean";
}

uint32_t value_;
};
}

TESTCASE(ComparableButNotBool)
{
define_class<ComparableButNotBool>("IsComparable").
define_constructor(Constructor<ComparableButNotBool, uint32_t>());

Class c = define_vector<std::vector<ComparableButNotBool>>("ComparableVector");

Object vec = c.call("new");
vec.call("push", ComparableButNotBool(1));
vec.call("push", ComparableButNotBool(2));
vec.call("push", ComparableButNotBool(3));

Object result = vec.call("delete", ComparableButNotBool(1));
ASSERT_EQUAL(Qnil, result.value());

result = vec.call("length");
ASSERT_EQUAL(3u, detail::From_Ruby<size_t>().convert(result));

result = vec.call("include?", ComparableButNotBool(2));
ASSERT_EQUAL(Qfalse, result.value());

result = vec.call("index", ComparableButNotBool(3));
ASSERT_EQUAL(Qnil, result.value());
}

TESTCASE(DefaultConstructable)
{
define_class<Comparable>("IsComparable").
Expand Down

0 comments on commit 326de94

Please sign in to comment.