Boost logo

Boost :

From: Dave Abrahams (abrahams_at_[hidden])
Date: 2000-02-03 07:41:17

on 2/3/00 5:28 AM, Aleksey Gurtovoy at alexy_at_[hidden] wrote:

> I have in mind a small (pretty??) class, which seems to me to be a yet
> another candidate to our boost utility library. I've seen it to be
> reinvented at least 2 times after I'd invented it myself , so I will be not
> much surprised if someone say 'I have this one too'..
> And even if you didn't, but you had ever written a code which assign some
> temporary value to a variable at some place in a block (usually at the
> beginning), and restore the old value at the end of it, I am sure you would
> appreciate this class :). It does exactly such thing - assign a new value to
> a variable, storing the old one within itself, and restore the original
> value in its destructor.

I once designed a more flexible variation on this called set_on_exit, which
would assign a value into a reference upon destruction. This lets you
restore the original value thusly:

    boost::set_on_exit<bool> on_exit(flag, flag);
    flag = false;

But also lets you do things like this:

    boost::set_on_exit<bool> on_exit(flag, true);

which sets flag to true regardless of its current state when the scope is
exited. That's useful for things like semaphores (detecting synchronous
re-entry to a section of code).

Also, even the name temporary_value is not great, IMO, because objects of
this class do not act as temporary values of their parameter type.


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