Boost logo

Boost :

Subject: Re: [boost] Proposed new RAII Library
From: Sergey Cheban (s.cheban_at_[hidden])
Date: 2012-09-14 19:33:11


14.09.2012 22:48, Sohail Somani пишет:

> Or in C++11:
>
> boost::scope_guard lolwhat( [&](){ CloseHandle(handle); } );
>
> ...
> lolwhat.dismiss();
What about
boost::exception_guard somename( [&](){ CloseHandle(handle); } );
that calls the function/lambda only if std::uncaught_exception() returns
true?

Another example:

extern "C" int SomeFunc( char **Ret1, char **Ret2 )
{
   try {
     *Ret1 = new char[100];
     boost::exception_guard Ret1Guard( [&](){ delete [] *Ret1; } );
     *Ret2 = new char[100];
     boost::exception_guard Ret2Guard( [&](){ delete [] *Ret2; } );

     //try to fill the buffers with the data

     return 0;
   }
   catch( std::exception &ex ) {
     Log(ex);
     return -1;
   }
}

Note that dismisses are not required, and you even can use some macro
magic to get rid of the meaningful guard variable names.

--
Best regards,
Sergey Cheban

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk