diff --git a/AK/MaybeOwned.h b/AK/MaybeOwned.h index 38e3c20d119..dd87626fb4d 100644 --- a/AK/MaybeOwned.h +++ b/AK/MaybeOwned.h @@ -32,6 +32,12 @@ public: MaybeOwned(MaybeOwned&&) = default; MaybeOwned& operator=(MaybeOwned&&) = default; + template U> + MaybeOwned(MaybeOwned&& other) + : m_handle(downcast(move(other.m_handle))) + { + } + T* ptr() { if (m_handle.template has()) @@ -57,7 +63,22 @@ public: bool is_owned() const { return m_handle.template has>(); } private: - Variant, T*> m_handle; + template + friend class MaybeOwned; + + template + using Handle = Variant, HT*>; + + template + Handle downcast(Handle&& variant) + { + if (variant.template has()) + return variant.template get(); + else + return static_cast&&>(move(variant.template get>())); + } + + Handle m_handle; }; }