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,375 of 33,346   
   DeMarcus to Michael Kilburn   
   Re: Will we ever be able to throw from a   
   06 Jun 12 17:02:30   
   
   d956475c   
   From: use_my_alias_here@hotmail.com   
      
   On 06/05/2012 09:51 PM, Michael Kilburn wrote:   
   > Hi,   
   >   
   > On Jun 4, 7:46 am, DeMarcus  wrote:   
   >> In Exceptional C++ by Herb Sutter, p.55, he states why throwing from   
   > destructors is a bad thing.   
   >>   
   >> The other day I came up with an argument why we may never be able to   
   > throw from a destructor.   
   > [skip]   
   >> Please give your comments and say whether you think my "proof" holds   
   > or not.   
   >   
   > Your proof is based on assumption that there is only one destructor.   
   > Unfortunately someone decided long ago that automatic destruction of   
   > local objects should be implemented by same function regardless how   
   > destruction happened (via leaving scope normally or via stack   
   > unwinding)... This led to unfortunate side-effect, namely -- you can't   
   > tell if your object is destroyed due to exception. This becomes   
   > painful when you code DB connection/transaction classes -- you   
   > normally want lifetime of these objects to mirror lifetime of their   
   > counterparts on the server and having ability to detect destruction-   
   > due-to-exception is very handy here.   
   >   
   > Anyway, idea is to have smth like this:   
   >   
   > struct Foo   
   > {   
   >       ~Foo();            // called when we leave the scope normally (and   
   > can throw)   
   >       ~~Foo() throw();   // called by stack unwinding   
   > };   
   >   
   > If ~~Foo() is not defined -- it points (aliases) to ~Foo(), which   
   > becomes nothrow. This is relatively simple mechanism, would be really   
   > nice to have it in C++.   
   >   
      
   Could you simulate that with std::uncaught_exception? Like so:   
      
   Foo::DtorFooUnwind() noexcept   
   {   
      // Do what you need to do when the stack is unwinding.   
   }   
      
   Foo::~Foo()   
   {   
      if( std::uncaught_exception() )   
      {   
         DtorFooUnwind();   
         return;   
      }   
      
      // Else, commit to DB or other close-down functionality.   
   }   
      
   See http://en.cppreference.com/w/cpp/error/uncaught_exception   
      
   Wouldn't this still cause problems for functionality expecting   
   destructors not to throw? Or can we with this solution rethink the way   
   we use RAII today?   
      
      
   Regards,   
   Daniel   
      
      
   --   
         [ 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