Neal Becker | 21 May 15:05

Re: constrained_value question

Robert Kawulak wrote:

>> 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;
>         else
>             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
>     stretchy_policy
>>::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<
>     cool_int,
>     value_generator<int, 0>,
>     value_generator<int, 100>,
>     value_generator<bool, true>,
>     value_generator<bool, true>,
>     throw_exception<>,
>     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 values.
> 
> I haven't compiled the code and can't tell it works for sure, but in
> general the idea seems to be valid. ;-)
> 
It wouldn't be part of the error_policy, since the extrema might be within
bounds and error_handler wouldn't then be invoked.

I was thinking maybe to extend within_bounds, either by inheritance or
aggregation?

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


Gmane