Re: bind guard ?
Ivan Novick wrote:
Hi Max,
Maxim Yegorushkin wrote:
You can make one yourself easily:
Do you have any link to what is a guard?
Sorry for newbie question.
I'm no expert, but to return the favor...
A guard is an object which makes sure an operation
is undone, for example, before execution leaves the
current function. Guards handle exception throwing
too.
A common guard is an automatic pointer:
autoptr<Foo> ap(new Foo());
which frees the Foo after it's no longer needed.
Another is a semaphore guard which gives back the
semaphore once execution leaves the mutex section:
boost::mutex m;
void func()
{
boost::mutex::scoped_lock l(m);
// mutex section
}
A common property of a guard is that it takes
the resource during construction (before giving it
back at destruction.) This ensures that no exception
can be thrown before getting the resource under the
protection of the guard.
The guard that I was looking for was a bit different.
With it, I'm not looking to protect a resource (so
it doesn't "take" anything during construction).
All I want is for a generic function to be called
before leaving an execution block.
To follow up with the previous posts, the ones
suggested weren't exactly what I was looking for
because they require a hard-to-specify local
variable:
scoped_guard<???> g = make_guard(boost::bind(function, arg1, arg2));
whereas all I wanted to write was:
scoped_guard g = ...
If I've misunderstood, I'd appreciate the clarification.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]