[Development] parented_ptr

Ville Voutilainen ville.voutilainen at gmail.com
Wed Nov 5 12:30:40 CET 2025


On Wed, 5 Nov 2025 at 12:11, Volker Hilsheimer via Development
<development at qt-project.org> wrote:
> And again, we need to make sure that object->createChild<QWidget>() is not possible; from what I see, that can be solved via SFINAE:
>
> class QObject …
> {
>     template <typename ChildType, typename ...Args,
>               std::enable_if_t<!std::is_base_of_v<QWidget, ChildType>, bool> = true
>              >
>     ChildType *makeChild(Args &&...args)
>     {
>         return new ChildType(std::forward<Args>(args)..., this);
>     }
> };
>
> (it’s enough for QWidget to be forward declared, which it is already in qobject.h)
>
> and then overloading this in QWidget without the constraint (a QObject can be a child of a QWidget).

I don't quite follow why you think you need any SFINAE for this. If
you try to construct a QWidget child with a
QObject::createChildObject(),
that will fail to compile anyway because it tries to pass a QObject*
parent to the QWidget constructor, and that
constructor takes a QWidget*. There is no implicit base*->derived* conversion.


More information about the Development mailing list