Allow comparing rcp<T> to rcp<U>

This commit is contained in:
Josh Holtrop 2026-02-24 17:10:01 -05:00
parent 11555ded36
commit 69ecab39f2
2 changed files with 18 additions and 4 deletions

View File

@ -147,14 +147,16 @@ public:
return ptr != nullptr; return ptr != nullptr;
} }
bool operator==(const rcp & other) const template <typename U>
bool operator==(const rcp<U> & other) const
{ {
return ptr == other.ptr; return static_cast<const void *>(ptr) == static_cast<const void *>(other.ptr);
} }
bool operator!=(const rcp & other) const template <typename U>
bool operator!=(const rcp<U> & other) const
{ {
return ptr != other.ptr; return static_cast<const void *>(ptr) != static_cast<const void *>(other.ptr);
} }
bool operator==(std::nullptr_t) const bool operator==(std::nullptr_t) const

View File

@ -181,6 +181,17 @@ void test_move_assignment_releases_existing()
assert(mybase_destruct == destructed_before + 2); assert(mybase_destruct == destructed_before + 2);
} }
void test_cross_type_comparison()
{
rcp<MyDerived> derived = MyDerived::create(1.5);
rcp<MyBase> base = derived;
assert(base == derived);
assert(!(base != derived));
rcp<MyBase> other = MyBase::create(1, 2);
assert(base != other);
assert(!(base == other));
}
void test_reset() void test_reset()
{ {
int destructed_before = mybase_destruct; int destructed_before = mybase_destruct;
@ -238,6 +249,7 @@ int main(int argc, char * argv[])
test_create(); test_create();
test_multi_construct_from_raw_pointers(); test_multi_construct_from_raw_pointers();
test_copy_assignment_decrements_previous_reference(); test_copy_assignment_decrements_previous_reference();
test_cross_type_comparison();
test_reset(); test_reset();
test_move_constructor(); test_move_constructor();
test_move_assignment(); test_move_assignment();