[Interest] Warning about QHash keys().toVector()

Thiago Macieira thiago.macieira at intel.com
Sun Jun 13 19:10:44 CEST 2021


On Sunday, 13 June 2021 09:20:31 PDT 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?

Create the QVector container, reserve the proper size, and then iterate over 
the actions hashing table inserting the keys.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel DPG Cloud Engineering





More information about the Interest mailing list