On 10/16/25 13:01, Schimkowitsch Robert wrote:
What about a smart pointer that
- Tracks it's QObject using QPointer internally
- In it's destructor, checks if the object still exists
- If so, checks if the object has a parent
--> It has a parent: Don't delete
--> It has no parent: Delete


You mean something like:


namespace Internal {

template<typename Type>
struct UniqueObjectPtrDeleter
{
    using pointer = QPointer<Type>;

    constexpr UniqueObjectPtrDeleter() noexcept = default;
template<typename UpType, typename = std::enable_if_t<std::is_convertible_v<UpType *, Type *>>> constexpr UniqueObjectPtrDeleter(const UniqueObjectPtrDeleter<UpType> &) noexcept
    {}

    constexpr void operator()(pointer p) const
    {
static_assert(!std::is_void_v<Type>, "can't delete pointer to incomplete type"); static_assert(sizeof(Type) > 0, "can't delete pointer to incomplete type");

        if (not p.parent())
            delete p.data();
    }
};

} // namespace Internal

template<typename Type>
using UniqueObjectPtr = std::unique_ptr<Type, Internal::UniqueObjectPtrDeleter<Type>>;

template<typename Type, typename... Arguments>
auto makeUniqueObjectPtr(Arguments &&...arguments)
{
return UniqueObjectPtr<Type>{new Type(std::forward<Arguments>(arguments)...)};
}


I think you can simply write your own deleter for std::unique_ptr.

--
Development mailing list
[email protected]
https://lists.qt-project.org/listinfo/development

Reply via email to