home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,245 of 33,346   
   Dave Harris to Daniel James   
   Re: stl clear and erase.   
   05 May 12 06:08:15   
   
   From: brangdon@cix.compulink.co.uk   
      
   daniel@me.invalid (Daniel James) wrote (abridged):   
   > In article   
   > <9e1ed9de-2aa1-49fb-9dd4-c99d9d205a98@v2g2000vbv.googlegroups.com>,   
   > Edward Rosten wrote:   
   > > Note that there isn't a single instruction emitted between BEFORE   
   > > and AFTER, in other words, the call to erase is entirely erased.   
   >   
   > That, surely, is only possible here because the optimizer can tell   
   > that the vector i is not accessed after the call to erase, so   
   > optimizing away the clear will not change the observable behaviour   
   > of the program.   
      
   It's also possible because the compiler knows destroying an int is a   
   no-op. For example:   
      
        // (I use a wrapper because I'm not sure int::~int() is   
        // valid syntax).   
        struct Int { int i; };   
      
        void destroy( Int *p, size_t count ) {   
            for (size_t i = 0; i != count; ++i)   
                p[i].Int::~Int();   
        }   
      
   The loop has no observable effects and can be optimised away   
   completely. I don't know if GCC does this, but from comments in this   
   thread I'd be surprised if it didn't. The same principle allows   
   std::vector::clear() to be optimised to constant time.   
      
   -- Dave Harris, Nottingham, UK.   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca