Library Idea

by Robert Ramey

Sometimes a certain value has to change only for a limited scope. This class wrapper saves a copy of the current state of some object, and resets the object’s state at destruction time, undoing any change the object may have gone through. Here’s and example:

#include <boost/state_saver.hpp>

void func(A & a)
    boost::state_saver<A> s(a);
    ... // alter state of a by calling non-const functions
    ... // call other functions
    // original state of a automatically restored on exit

The boost serialization library currently includes such a component but has it never been formally accepted, it’s currently an implementation detail of the boost serialization library.

There are 0 comments

Comment on This Page