Boost logo

Boost :

From: Joseph Gottman (joegottman_at_[hidden])
Date: 2000-02-15 23:23:17


----- Original Message -----
From: Aleksey Gurtovoy <alexy_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Tuesday, February 15, 2000 7:35 AM
Subject: [boost] Re: yet another utility class(es)

> Here it is. Or, rather, here they are ;) - 'scoped_value' and
> 'deffered_value' classes. The second one is an implementation of Dave's
> 'set_on_exit' class, but (as you see :) I gave it another name. Of course,
a
> need for this class and especially the name itself is a big discussion
> point. But *I* feel both of them will be useful - each in its own way.
>
> I've also uploaded the code to the vault to 'scoped_value' directory. So,
> the code itself (without comments):
>
> namespace boost {
> namespace details {
>
> template<class T>
> class auto_restore
> {
> protected:
> typedef auto_restore<T> inherited;
>
> auto_restore( T& variable, const T& value )
> : variable_( variable )
> , value_( value ) {}
>
> ~auto_restore() { variable_ = value_; }
>
> private:
> const T value_;
> T& variable_;
> };
> } // namespace details
>

          Shouldn't value_ be of type T, not const T &? Suppose I have code
like
int foo = 3;
    if (1) { /* New scope.
    auto_restore<int> saveFoo(foo, foo);
    foo = 4;
}
cout << foo;

Since saveFoo.value_ is just an alias for foo, the destructor of saveFoo
will just set foo = foo, and foo will end up "restored" to the value 4, not
3 like you would expect.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk