Boost logo

Boost :

From: Robert Kawulak (tigrisek_at_[hidden])
Date: 2006-01-18 08:08:36


  Some time ago there was a discussion on the Constrained Types
library. Unfortunately other duties didn't let me work on it for
a long time (I apologise for this long break), but recently I
started the work again. As a reminder, or short description for
those who missed the discussion, there's a brief summary of usage
of the library at the end of this message.

  I need help in determining the most common use cases of the
library. Therefore I ask anybody who ever needed such a facility
to write what exactly he needed it for, what were his
expectations and requirements for functionality and efficiency.
In particular, answers for the following questions would be
useful (but all opinions and suggestions are valuable):

- Is it worth to make different implementation of
increment/decrement functions for integer types to make them much
more efficient than the generic versions? This needs complicating
the design a little bit (but not the usage). OTOH only ++/--
would be optimised, other operators not. So is ++/-- operation
actually more common than the others that it's worth it?

- Is such a feature likely to be needed: a possibility to select
different out-of-bounds policies for both the bounds, e.g. throw
when the lower bound is exceeded, clip when upper bound is

- How often would people need this library to work with types
other than integral or floating-point? What kind of types would
it be (cheap/expensive to copy)?

Any answers, suggestions, advice and questions are welcome.

Best regards,


struct is_even {
  bool operator()(int v) const
    { return (v % 2) == 0; }

constrained<int, is_even>::type even_int(0);
even_int += 4; // OK
even_int = 1; // error - throws

BOOST_DEFINE_VALUE_GENERATOR(abc_type, std::string, "abc");
BOOST_DEFINE_VALUE_GENERATOR(xyz_type, std::string, "xyz");

bounded<std::string, abc_type, xyz_type>::type my_string; //
my_string == "abc"
s = "x"; // OK
s += "zz"; // throws - "xzz" is not within the range

bounded_int<int, 0, 5, error::do_nothing>::type my_int; // my_int
== 0
my_int++; // OK
my_int *= 100; // invalid operation ignored, still my_int == 1

wrapping_int<unsigned_int, 5, 10>::type wraps; // wraps == 5
wraps--; // wraps == 10
wraps += 2; // wraps == 6

saturating<std::string, abc_type, xyz_type>::type clips; // clips
== "abc"
clips = "z"; // clips == "xyz" - value clipped to the upper bound

  These are just the simpliest examples. Other features are:
- policy-based design allowing customizations like different
constraint, error or out-of-bounds policy classes,
- any of the bounds may be excluded from the allowed set of
values (open ranges),
- bounds may be adjustable at run-time or defined at
- and some more I can't remember at the moment ;-)

Zmyslowa czerwien... >>>

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