[Development] parented_ptr

Schimkowitsch Robert Robert.Schimkowitsch at andritz.com
Thu Oct 16 14:24:36 CEST 2025


Looks reasonable. I was briefly confused why you'd still use enable_if, but I guess there are still people using C++17...

However, this implies a possible race condition between checking the parent and actually deleting the object. Not sure there is a way to solve that, though.

Kind regards

Robert

> -----Original Message-----
> From: Development <development-bounces at qt-project.org> On Behalf Of
> Marco Bubke via Development
> Sent: Thursday, 16 October 2025 13:23
> To: development at qt-project.org
> Subject: Re: [Development] parented_ptr
>
> CAUTION: External email. Do not click on links or open attachments unless
> you know the sender and that the content is safe.
>
>
> 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
> Development at qt-project.org
> https://lists.qt-/
> project.org%2Flistinfo%2Fdevelopment&data=05%7C02%7Crobert.schimkowit
> sch%40andritz.com%7C81c485b12e3745be063108de0ca6ae7e%7C6785298fe
> 857464b9b4b807178402632%7C1%7C0%7C638962107239635552%7CUnkno
> wn%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsI
> lAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdat
> a=PkYlq%2F5hYHlGsOBkQ6HLCu3dmWz6Kdbm9rITNFu383A%3D&reserved=0
________________________________

This message and any attachments are solely for the use of the intended recipients. They may contain privileged and/or confidential information or other information protected from disclosure. If you are not an intended recipient, you are hereby notified that you received this email in error and that any review, dissemination, distribution or copying of this email and any attachment is strictly prohibited. If you have received this email in error, please contact the sender and delete the message and any attachment from your system.

ANDRITZ HYDRO GmbH


Rechtsform/ Legal form: Gesellschaft mit beschränkter Haftung / Corporation

Firmensitz/ Registered seat: Wien

Firmenbuchgericht/ Court of registry: Handelsgericht Wien

Firmenbuchnummer/ Company registration: FN 61833 g

DVR: 0605077

UID-Nr.: ATU14756806


Thank You
________________________________


More information about the Development mailing list