diff --git a/AK/NonnullOwnPtr.h b/AK/NonnullOwnPtr.h index 622290578a..a9dd0cc474 100644 --- a/AK/NonnullOwnPtr.h +++ b/AK/NonnullOwnPtr.h @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -51,6 +52,7 @@ public: NonnullOwnPtr(AdoptTag, T& ptr) : m_ptr(&ptr) { + static_assert(!is_ref_counted((const T*)nullptr), "Use RefPtr<> for RefCounted types"); } NonnullOwnPtr(NonnullOwnPtr&& other) : m_ptr(other.leak_ptr()) diff --git a/AK/OwnPtr.h b/AK/OwnPtr.h index ed351be060..2ece450e81 100644 --- a/AK/OwnPtr.h +++ b/AK/OwnPtr.h @@ -27,16 +27,18 @@ #pragma once #include +#include namespace AK { template class OwnPtr { public: - OwnPtr() {} + OwnPtr() { } explicit OwnPtr(T* ptr) : m_ptr(ptr) { + static_assert(!is_ref_counted((const T*)nullptr), "Use RefPtr<> for RefCounted types"); } OwnPtr(OwnPtr&& other) : m_ptr(other.leak_ptr()) diff --git a/AK/RefCounted.h b/AK/RefCounted.h index 361e601286..9804940547 100644 --- a/AK/RefCounted.h +++ b/AK/RefCounted.h @@ -100,6 +100,16 @@ public: } }; +static constexpr bool is_ref_counted(const RefCountedBase*) +{ + return true; +} + +static constexpr bool is_ref_counted(...) +{ + return false; +} + } using AK::RefCounted;