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,399 of 33,346   
   Stefan =?utf-8?Q?Gro=C3=9Fe?= Pawig to DeMarcus   
   Re: Will we ever be able to throw from a   
   10 Jun 12 10:38:48   
   
   From: usenet@stegropa.de   
      
   DeMarcus  writes:   
      
   > On 06/05/2012 09:51 PM, Michael Kilburn wrote:   
   >> 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.   
   > }   
      
   Your Foo also has to remember whether it had been constructed during   
   stack unwinding.   
      
   This has already been discussed some years ago, either here or over in   
   comp.lang.c++.  I don't have a message reference, but I found the   
   following code lying around on my harddrive (from August 2007):   
      
   --- 8< ---   
   #include    
   #include    
   #include    
      
   struct Unwind   
   {   
      bool constructedDuringException;   
      
      Unwind() : constructedDuringException(std::uncaught_exception()) {}   
      
      bool stack_unwinding() const   
        {   
          return !constructedDuringException && std::uncaught_exception();   
        }   
   };   
      
   struct Lonely : private Unwind {   
        ~Lonely()   
        {   
          std::cout << "~Lonely(): uncaught=" << std::uncaught_exception()   
                    << ", unwinding=" << stack_unwinding() << std::endl;   
        }   
   };   
      
   struct Inner : private Unwind {   
        ~Inner()   
        {   
          std::cout << "~Inner(): uncaught=" << std::uncaught_exception()   
                    << ", unwinding=" << stack_unwinding() << std::endl;   
          assert( stack_unwinding() == false );   
        } // contrary to uncaught_exception   
      
   };   
      
   struct Outer : private Unwind {   
      ~Outer()   
        {   
          std::cout << "~Outer(): uncaught=" << std::uncaught_exception()   
                    << ", unwinding=" << stack_unwinding() << std::endl;   
          assert( stack_unwinding() == true ); // like uncaught_exception   
          Inner in;   
        }   
   };   
      
   void fun()   
   {   
      Outer out;   
      throw std::exception();   
   }   
      
   int main()   
   {   
      Lonely l;   
      try { fun(); } catch (...) {}   
   }   
   --- 8< ---   
      
   Regards,   
      Stefan   
      
      
   --   
         [ 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