For example, can it be used in Qt for the following?
gsl::owner<QWidget*> w{new QWidget{parent}}
In this example, the ownership is shared by the new-site and the parent
, because the code who has new
-ed the object can delete
w
, and W
's destructor will take itself out of parent
children list. However, if w
is not deleted at the new-site, then parent
will delete it in its destructor.
gsl::owner
be used for it?Is this an example of shared ownership
No, you have two places claiming unique ownership.
can
gsl::owner
be used for it
If w
can outlive the object pointed to by parent
, then it might become invalid, leading to undefined behaviour.
If you want a pointer type that interfaces with Qt's parent-child ownership, use QPointer