[Development] unique_ptr and Qt

Иван Комиссаров abbapoh at gmail.com
Sat Jun 16 11:29:48 CEST 2018


It would be nice if makeChild returned a some kind of «stupid» smart pointer (gsl::not_null maybe?) to indicate that object is not owned by holder (compare with raw pointer where the ownership is not known without reading doc):
template <class T, class... Args> QNonOwningPointer<T> QObject::makeChild(Args&&... args);

Another thought is that it may make sense to invert child->setParent(parent) function to something like this:
template <class T> void QObject::addChild(std::unique_ptr<T> &&p);
template <class T> void QObject::addChild(const QNonOwningPointer <T> p);

So the code with std::shared_pointer won’t compile:

auto o = std::make_shared<QObject>(«object»);
parent->addChild(o); // oops, how do we suppose «share» ownership between user and QObject?

However, it is ok with unique_ptr or «stupid» ptr:

auto o = std::make_unique<QObject>(«object»);
parent->addChild(std::move(o)); // ok, ownership transfered

QNonOwningPointer<QObject> o = QObject::makeChild<QObject>(parent1, «object»);
parent2->addChild(o); // ok, ownership transfered from parent1 to parent2

My 2 cents.


> 15 июня 2018 г., в 10:15, Olivier Goffart <olivier at woboq.com> написал(а):
> 
> On 2018-06-05 14:40, Daniel Teske wrote:
>> Hi,
>> I've done some research into how supporting unique_ptr in Qt might look like.
>> I have a patch which adds apis using unique_ptr for ownership transfer to almost all of QtBase and a patch for Qt Creator to show how using those apis looks like.
>> Qt: https://codereview.qt-project.org/231543
>> Qt Creator: https://codereview.qt-project.org/231545
> > [...]
> 
> Hi Daniel,
> 
> Thanks for doing this.
> 
> Here is what i suggest to make the change less intrusive:
> For the constructors themself in every classes.
> 
>  // Removed the '=nullptr' default parent, and added QT_UNSAFE_API
>  QT_UNSAFE_API explicit QLineEdit(QWidget *parent);
>  QT_UNSAFE_API explicit QLineEdit(const QString &, QWidget *parent);
>  // Added new non-deprecated constructor not taking parent.
>  explicit QLineEdit(const QString & = {});
> 
> 
> //The QObject::makeChild method:
> template <class T, class... Args> T* QObject::makeChild(Args&&... args)
> {
>  auto n = new T(std::forward<Args>(args)...);
>  n->setParent(this);
>  return n;
> }
> 
> 
> That way, std::make_unique still work, and constructing objects on the stack does not change.
> 
> For the cases where the parent in the constructor has another meaning, we would need another factory function (makeChild) in the relevant class. Or interept the ParentChanged, or reimplement setParent in that class.
> 
> What do you think?
> 
> -- 
> Olivier
> 
> Woboq - Qt services and support - https://woboq.com - https://code.woboq.org
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development




More information about the Development mailing list