Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to avoid Clazy warning "Allocating an unneeded temporary container" when joining keys?
Forum Updated to NodeBB v4.3 + New Features

How to avoid Clazy warning "Allocating an unneeded temporary container" when joining keys?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 807 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jens G
    wrote on last edited by
    #1

    For some message I need a list of QMap keys as a string. How can following (simplified) code be improved to avoid said Clazy warning?

    const QMap<QString, int> shoppingList {{"Bananas", 5}, {"Bread", 1}, {"Milk", 2}};
    QString keysText = shoppingList.keys().join(", ");
    
    JonBJ D 2 Replies Last reply
    0
    • J Jens G

      For some message I need a list of QMap keys as a string. How can following (simplified) code be improved to avoid said Clazy warning?

      const QMap<QString, int> shoppingList {{"Bananas", 5}, {"Bread", 1}, {"Milk", 2}};
      QString keysText = shoppingList.keys().join(", ");
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Jens-G
      I think it does not like you generating keys() and then iterating it to join(). I think it would like you to write something like:

      for (const auto & element : shoppingList )
          keysText += ...
      

      or use QMap::key_iterator QMap::keyBegin() const or similar iterator.

      Though how that is as convenient as being able to use join(", ") I don't know/see! Unless an expert knows better. Sounds to me like you/we may know better than clazy here :) [Oh, unless you are using a million+ keys, then this is good advice :) ]

      1 Reply Last reply
      2
      • J Jens G

        For some message I need a list of QMap keys as a string. How can following (simplified) code be improved to avoid said Clazy warning?

        const QMap<QString, int> shoppingList {{"Bananas", 5}, {"Bread", 1}, {"Milk", 2}};
        QString keysText = shoppingList.keys().join(", ");
        
        D Offline
        D Offline
        DerReisende
        wrote on last edited by DerReisende
        #3

        @Jens-G As @JonB already said clazy complains about the temporary QList returned by .keys().
        To prevent it you could either use QMapIterator or QMap::key_iterator to join your code.
        I made some quick and dirty "performance" measurements on my machine and STL-style key iterators seem to be consistently faster than the other methods:

        .join():
        T1:  "Bananas, Bread, Milk"
        Timing:  3900  nanoseconds.
        QMapIterator:
        T2:  "Bananas, Bread, Milk"
        Timing:  4200  nanoseconds.
        STL-style:
        T3:  "Bananas, Bread, Milk"
        Timing:  1500  nanoseconds.
        
        

        Done on Windows 11, Visual C++ 2022, Qt 6.5.0 Release mode with AVX2

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved