On Sun, Jun 13, 2021 at 10:35 PM André Pönitz <apoen...@t-online.de> wrote:
> On Sun, Jun 13, 2021 at 06:20:31PM +0200, Kevin André wrote:
> >
> > I have the following piece of code:
> >
> >     QVector<int> CompatibilityInterfaceImpl::getActionIds() const
> >     {
> >         return _actions.keys().toVector();   // _actions is a QHash
> >     }
> >
> > In Qt Creator this generates the following warning:
> >    allocating an unneeded temporary container [clazy-container-anti-pattern]
> >
> > How can I avoid the warning in this case?
> >
> > I am using Qt 5.12, so I return a QVector instead of a QList because
> > using the old (Qt 5) QList is discouraged. Unfortunately many Qt API's
> > still use QList for nearly everything, so I am faced with a difficult
> > choice:
> >
> >   1) go back to using QList
> That would be my advice.

Well, I decided not to use QList because I like things to be
consistent and use QVector everywhere.

> While QList never was the atrocity some people wanted us to believe [1],
> you actually have one of the rare cases here were Q(5)Vector really
> takes less memory than Q(5)List on 64bit machines, roughly 4 bytes
> times the number of items in your _actions container.

I am currently using mingw 32-bit, does that make much of a difference
for the comparison?

In Qt 5, QList stores pointers to the elements while QVector stores
the elements directly, is that correct? In that case, wouldn't the
difference be 8 bytes times the element count if the element is a
64-bit int?

> But given that your application is apparently fine with using QHash::keys
> here it is safe to say that you will not observe any difference.

True. For this particular piece of code I even expect the number of
elements to be less than 10 most of the time :-)
But my question was more about what's the best practice.


Interest mailing list

Reply via email to