Boost logo

Boost :

From: Robert Kawulak (kawulak_at_[hidden])
Date: 2008-05-20 15:19:59

> From: Neal Becker
> One thing might be useful is to have the option to record minimum and
> maximum values that are used during a given computation.

You can write error policy:

struct stretchy_policy {
    template <typename V, typename C>
    void operator () (const V &, const V & new_val, C & constraint)
        if( constraint.is_below(new_val) )
            constraint.lower_bound() = new_val;
            constraint.upper_bound() = new_val;

And then use it like this:

// int wrapper that remembers extreme values assigned to it:
typedef bounded<
    int, int, int,
    value_generator<bool, true>, // bounds have to be included if we
    value_generator<bool, true>, // want them to indicate valid values
>::type cool_int;

cool_int i(4, cool_int::constraint_type(4, 4)); // note: should initialise
bounds too
i = -1;
i = 10;
assert( i.constraint().lower_bound() == -1 && i.constraint().upper_bound() == 10

Of course this will not allow you to do bounds-checking, but you can nest
cool_int in another bounded type:

typedef bounded<
    value_generator<int, 0>,
    value_generator<int, 100>,
    value_generator<bool, true>,
    value_generator<bool, true>,
    std::less<int> // needed to avoid problems with conversions
>::type splendid_int;

Now you have int that must be in the range [0, 100] and remembers its extreme

I haven't compiled the code and can't tell it works for sure, but in general the
idea seems to be valid. ;-)

Best regards,

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