AK: Explicitly delete NonnullRefPtr::operator=(RefPtr).

This gives us much better error messages when you try to use them.
Without this change, it would complain about the absence of functions
named ref() and deref() on RefPtr itself. With it, we instead get a
"hey, this function is deleted" error.

Change operator=(T&) to operator=T(const T&) also, to keep assigning
a const T& to a NonnullRefPtr working.
This commit is contained in:
Andreas Kling 2019-08-02 11:51:28 +02:00
parent 6db879ee66
commit 06f82901b7
Notes: sideshowbarker 2024-07-19 12:56:44 +09:00

View file

@ -8,6 +8,8 @@ namespace AK {
template<typename T>
class OwnPtr;
template<typename T>
class RefPtr;
template<typename T>
inline void ref_if_not_null(T* ptr)
@ -89,6 +91,13 @@ public:
template<typename U>
NonnullRefPtr& operator=(const OwnPtr<U>&) = delete;
template<typename U>
NonnullRefPtr(const RefPtr<U>&) = delete;
template<typename U>
NonnullRefPtr& operator=(const RefPtr<U>&) = delete;
NonnullRefPtr(const RefPtr<T>&) = delete;
NonnullRefPtr& operator=(const RefPtr<T>&) = delete;
NonnullRefPtr& operator=(const NonnullRefPtr& other)
{
if (m_ptr != other.m_ptr) {
@ -129,11 +138,11 @@ public:
return *this;
}
NonnullRefPtr& operator=(T& object)
NonnullRefPtr& operator=(const T& object)
{
if (m_ptr != &object) {
deref_if_not_null(m_ptr);
m_ptr = &object;
m_ptr = const_cast<T*>(&object);
m_ptr->ref();
}
return *this;