Boost logo

Boost :

Subject: Re: [boost] [constrained_value] New version
From: Robert Kawulak (kawulak_at_[hidden])
Date: 2008-11-18 20:02:06


Hi Neal,

> From: Neal Becker
> I thought to use 'unconstrained', but this isn't really what
> I need. I have a fixed_pt integer class,
>
> //! signed fixed pt
> template<int int_bits, int frac_bits, typename base_type=int,
> typename error_policy=cv::throw_exception<>, typename
> round_policy=rnd<base_type> >
> struct fixed_pt :
> ...
> typedef typename cv::bounded_int<base_type, min,
> max,error_policy>::type val_t;
>
> When I want to turn off checking, it seems the thing to do is
> use an error_policy which ignores the error. I've been using
> this, which I got from you some time ago:
>
> struct no_error_policy {
> template <typename V, class C>
> void operator () (V & old_val, const V & new_val, C &)
> {
> old_val = new_val;
> }
> };
>
>
> So I can write:
> fixed_pt<16,5,int,cv::no_error_policy> f;
>
> Maybe this is a useful addition to the lib, or at least as an example?

OK, now I think I see what you want to do.

I don't think using this policy is a good idea for two reasons:
- as I've written before, it is not a correct error policy in a general case. It
will break the bounded object's invariant (and fire an assertion) if you assign
an invalid value;
- it will make it harder for the compiler to optimize the unnecessary checks
away.

What I would suggest is using bounded_int when there is actually a need for
error checking, and using unconstrained when there should be no error-checking:

        struct no_error_checking {}; // a tag to indicate no checks at all

        template<... typename base_type = int, typename error_policy =
cv::throw_exception<> ...>
        struct fixed_pt {

                typedef typename if_<
                        is_same<error_policy, no_error_checking >::type,
                        cv::unconstrained<base_type>::type,
                        cv::bounded_int<base_type, min, max, error_policy>::type
>::type val_t;

Or, if there are no issues with the interface compatibility, you may use the
underlying value type directly:

                typedef typename if_<
                        is_same<error_policy, no_error_checking >::type,
                        base_type,
                        cv::bounded_int<base_type, min, max, error_policy>::type
>::type val_t;

Hope this helps,
Robert


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