|
Boost : |
Subject: Re: [boost] [exception] uncaught_exception_count, scope(failure), scope(success)
From: Evgeny Panasyuk (evgeny.panasyuk_at_[hidden])
Date: 2013-02-05 18:25:41
06.02.2013 2:57, Emil Dotchevski:
> I mean that in D/Java/C#/etc you don't know when the destructor will
> get called automatically, which means that you're stuck with having to
> manually keep track of all allocated resources exactly like in C,
> except for memory.
scope(exit) (in C++) has similar behaviour to normal destructors, and in
fact it is implemented on top of destructors.
scope(failure) and scope(success) differs from scope(exit) only by
additional condition - code in scope(failure) is called only when scope
is exited by exception, and code in scope(success) is called only during
"normal" scope exit.
D's documentation says (
http://dlang.org/statement.html#ScopeGuardStatement ) :
"The ScopeGuardStatement executes NonEmptyOrScopeBlockStatement at the
close of the current scope, rather than at the point where the
ScopeGuardStatement appears."
Regarding C# and Java: they have some "workarounds" for resource
management: C# has "using", and Java has "try-with-resources".
Regarding C: there are also approaches to manage resources
automatically, but that's another story.
>> While scope(exit) is some kind of ad-hoc replacement for RAII wrappers,
>> scope(failure) and scope(success) are not.
>> Nowadays scope(failure) and scope(success) are emulated in C++ via
>> ScopeGuard idiom.
>
> Could you clarify what does uncaught_exceptions_count() use look like?
> How is it different scope guard?
For instance, check first example at Boost.ScopeExit documentation
(C++98 syntax):
http://www.boost.org/doc/libs/1_53_0/libs/scope_exit/doc/html/index.html
void world::add_person(person const& a_person) {
bool commit = false;
persons_.push_back(a_person);
BOOST_SCOPE_EXIT(&commit, &persons_) {
if(!commit)
persons_.pop_back();
} BOOST_SCOPE_EXIT_END
// ...
commit = true;
}
Using SCOPE_FAILURE it would became (C++98 syntax):
void world::add_person(person const& a_person) {
persons_.push_back(a_person);
SCOPE_FAILURE(&persons_) {
persons_.pop_back();
} SCOPE_FAILURE_END
// ...
}
Also, there are side-by-side examples at
https://github.com/panaseleus/stack_unwinding#d-style-scope-guardsactions .
-- Evgeny Panasyuk
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk