From 69ecab39f264ce1a5faa69f5648b38b3e94e0641 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 24 Feb 2026 17:10:01 -0500 Subject: [PATCH] Allow comparing rcp to rcp --- include/rcp.h | 10 ++++++---- test/tests.cpp | 12 ++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/rcp.h b/include/rcp.h index 8e8f595..fbcd38c 100644 --- a/include/rcp.h +++ b/include/rcp.h @@ -147,14 +147,16 @@ public: return ptr != nullptr; } - bool operator==(const rcp & other) const + template + bool operator==(const rcp & other) const { - return ptr == other.ptr; + return static_cast(ptr) == static_cast(other.ptr); } - bool operator!=(const rcp & other) const + template + bool operator!=(const rcp & other) const { - return ptr != other.ptr; + return static_cast(ptr) != static_cast(other.ptr); } bool operator==(std::nullptr_t) const diff --git a/test/tests.cpp b/test/tests.cpp index 2e34fde..d4dac69 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -181,6 +181,17 @@ void test_move_assignment_releases_existing() assert(mybase_destruct == destructed_before + 2); } +void test_cross_type_comparison() +{ + rcp derived = MyDerived::create(1.5); + rcp base = derived; + assert(base == derived); + assert(!(base != derived)); + rcp other = MyBase::create(1, 2); + assert(base != other); + assert(!(base == other)); +} + void test_reset() { int destructed_before = mybase_destruct; @@ -238,6 +249,7 @@ int main(int argc, char * argv[]) test_create(); test_multi_construct_from_raw_pointers(); test_copy_assignment_decrements_previous_reference(); + test_cross_type_comparison(); test_reset(); test_move_constructor(); test_move_assignment();