Add operator==() and operator!=()

This commit is contained in:
Josh Holtrop 2026-02-24 14:44:38 -05:00
parent 47273edf6e
commit 4a1a0c8855
2 changed files with 25 additions and 0 deletions

View File

@ -105,6 +105,26 @@ public:
return ptr != nullptr;
}
bool operator==(const rcp<T> & other) const
{
return ptr == other.ptr;
}
bool operator!=(const rcp<T> & other) const
{
return ptr != other.ptr;
}
bool operator==(std::nullptr_t) const
{
return ptr == nullptr;
}
bool operator!=(std::nullptr_t) const
{
return ptr != nullptr;
}
template <typename... Args>
static rcp<T> create(Args&&... args)
{

View File

@ -68,14 +68,19 @@ void test_dereference()
assert(mybase->y == 3);
assert((*mybase).x == 2);
assert((*mybase).y == 3);
rcp<MyBase> mybase2 = mybase;
assert(mybase == mybase2);
assert(!(mybase != mybase2));
}
void test_booleans()
{
rcp<MyBase> mybase = MyBase::create(2, 3);
assert(mybase);
assert(mybase != nullptr);
rcp<MyDerived> myderived;
assert(!myderived);
assert(myderived == nullptr);
}
void test_create()