Subject: [Boost-bugs] [Boost C++ Libraries] #5260: scope_guard safe_execute could support try/catch when BOOST_NO_EXCPTIONS not defined
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2011-03-03 20:36:49
#5260: scope_guard safe_execute could support try/catch when BOOST_NO_EXCPTIONS
not defined
------------------------------+---------------------------------------------
Reporter: z_gerg@⦠| Owner: joaquin
Type: Bugs | Status: new
Milestone: To Be Determined | Component: multi_index
Version: Boost 1.46.0 | Severity: Problem
Keywords: |
------------------------------+---------------------------------------------
Hi,
Since I use boost library for many things, I thought I would take
advantage of the scope guard implementation in
boost/multi_index/detail/scope_guard.hpp. I know that is not standard
practice, but thought it would be better then implementing my own ;-)
However I discovered the hard way (got an unhandled exception) that
safe_execute does not support try/catch. From the comments
* - safe_execute does not feature a try-catch protection, so we can
* use this even if BOOST_NO_EXCEPTIONS is defined.
I verified that this is still the case in the latest release version. (I
use an older version).
It seems it could support try/catch while still honoring
BOOST_NO_EXCEPTIONS when defined by wrapping the try and catch portions in
#ifdefs as follows
template<typename J>
static void safe_execute(J& j)
{
if(!j.dismissed_)
#ifndef BOOST_NO_EXCEPTIONS
try {
#endif
j.execute();
#ifndef BOOST_NO_EXCEPTIONS
} catch (...) {
}
#endif
}
By doing that when using exceptions it will work as the original code this
is based on works. And when BOOST_NO_EXCEPTIONS is defined it will work as
it does now.
The following example code will generate an unhandled exception with out
the above mods. The same example with the above mods will also generate
the unhandled exception if BOOST_NO_EXCEPTIONS is defined (you can
uncomment the #define BOOST_NO_EXCEPTIONS to test that). It will not
generate an unhandled exception when BOOST_NO_EXCEPTIONS is not defined.
// #define BOOST_NO_EXCEPTIONS
#include <iostream>
#include <boost/multi_index/detail/scope_guard.hpp>
void bar()
{
throw std::exception("testing");
}
void foo()
{
boost::multi_index::detail::scope_guard barGuard =
boost::multi_index::detail::make_guard(&bar);
}
int main(int argc, char* argv[])
{
foo();
return 0;
}
-- Ticket URL: <https://svn.boost.org/trac/boost/ticket/5260> Boost C++ Libraries <http://www.boost.org/> Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:05 UTC