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 fix foreach warning: allocating an unneeded temporary container [-Wclazy-container-anti-pattern]

How to fix foreach warning: allocating an unneeded temporary container [-Wclazy-container-anti-pattern]

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 5.8k 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.
  • P Offline
    P Offline
    petr.bena
    wrote on last edited by
    #1

    I am getting lot of these warning for this code:

            foreach (QString key, target->PropertyBag.keys()) // this is where I see it
            {
                if (!this->PropertyBag.contains(key))
                    this->PropertyBag.insert(key, target->PropertyBag[key]);
                else
                    this->PropertyBag[key] = target->PropertyBag[key];
            }
    

    How should I change it so that I don't see the warning? Saving a list of keys in QList would probably dismiss the warning as well but it would also create a temporary container.

    kshegunovK 1 Reply Last reply
    2
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by dheerendra
      #2

      No point in calling same method again and again. Try the following.

      QList keys = target->PropertyBag.keys();

      foreach (QString key, keys){
      
      }
      

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      2
      • P Offline
        P Offline
        petr.bena
        wrote on last edited by
        #3

        is there a significant performance difference between the two?

        1 Reply Last reply
        0
        • P Offline
          P Offline
          petr.bena
          wrote on last edited by petr.bena
          #4

          also in my case PropertyBag is in most cases an empty hash, so I am thinking that maybe putting if (!PropertyBag.isEmpty()) in front would avoid unnecessary copy of empty qhash? This code is called in a loop for large amount of items, each item has its own PropertyBag.

          1 Reply Last reply
          0
          • P petr.bena

            I am getting lot of these warning for this code:

                    foreach (QString key, target->PropertyBag.keys()) // this is where I see it
                    {
                        if (!this->PropertyBag.contains(key))
                            this->PropertyBag.insert(key, target->PropertyBag[key]);
                        else
                            this->PropertyBag[key] = target->PropertyBag[key];
                    }
            

            How should I change it so that I don't see the warning? Saving a list of keys in QList would probably dismiss the warning as well but it would also create a temporary container.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #5
            for (auto & element : qAsConst(target->propertyBag))
                PropertyBag.insert(key, element.first);
            

            foreach is deprecated.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            3

            • Login

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