Boost logo

Boost :

Subject: Re: [boost] Proposed new RAII Library
From: Andrew Sandoval (sandoval_at_[hidden])
Date: 2012-09-12 15:36:35

Lorenzo Caminiti <lorcaminiti <at>> writes:
> Seems similar to:
> #include <functional>
> struct scope_exit {
> scope_exit(std::function<void (void)> f) : f_(f) {}
> ~scope_exit(void) { f_(); }
> private:
> std::function<void (void)> f_;
> };
> void world::add_person(person const& a_person) {
> bool commit = false;
> persons_.push_back(a_person);
> scope_exit on_exit1([&commit, this](void) { // Use C++11 lambda.
> if(!commit) persons_.pop_back(); // `persons_` via captured `this`.
> });
> // ...
> commit = true;
> }
> I personally wouldn't need a lib for such a scope_exit class--it's trivial.

Yes, it is very similar to the scope_exit 'alternative' example. (The keyword
there is example, and you have to hunt to find this example.)

Yes, it is trivial to write, but so is boost::noncopyable and because it exists
in the library it gets used and re-used instead of being reinvented over and
over again. (Isn't that the point to a library?)

Regardless of how trivial it may be, making it a standard will increase the use
of RAII and thereby increase code quality in many cases. The fact that it is
simple will mean more engineers will learn to use it and improve not just the
code they write, but the way they think while writting code -- because employing
RAII in my experience has that side benefit. When you contemplate a resource's
lifetime at inception you reduce a lot of common mistakes, like holding locks
longer than necessary, leaking objects/memory, etc.

Andrew Sandoval

Boost list run by bdawes at, gregod at, cpdaniel at, john at