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,329 of 33,346   
   Wil Evers to Zoltan Juhasz   
   Re: Broken interaction between std::prio   
   24 May 12 01:40:43   
   
   From: bouncer@dev.null   
      
   Zoltan Juhasz wrote:   
      
   > On Monday, 21 May 2012 15:28:09 UTC-4, Gene Bushuyev  wrote:   
   >> To avoid default constructing an object one would need the function   
   >> returning by value, how about simply modifying pop to return a value:   
   >>   
   >> template   
   >> T priority_queue::pop()   
   >> {   
   >>     T tmp(move(container.front()));   
   >>     container.erase(container.begin());   
   >>     make_heap(container.begin(), container.end(), compare);   
   >>     return tmp;   
   >> }   
   >>   
   >> It will not break existing code that relies on copy and assumes pop()   
   >> doesn't return anything.   
   >   
   > Unfortunately this implementation does not provide strong exception   
   > safety guarantees, e.g. if an exception is thrown when you return tmp,   
   > the container has been already modified, in an irreversible way, and   
   > the returned element is lost.   
      
   Which is why, as Seungbeom Kim suggested, this version of pop() should   
   only be enabled if T has a non-throwing move constructor (or, if   
   it doesn't have a move constructor, a non-throwing copy constructor).   
      
   - Wil   
      
      
   --   
         [ 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