$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
From: Jeff Garland (jeff_at_[hidden])
Date: 2004-05-10 09:46:10
On Mon, 10 May 2004 14:00:41 +0100, Reece Dunn wrote
> Is there a boost library that allows...
No, but there is a template in date_time that does something close.  It's
called constrained_value.
When an out of range value is found during construction or assignment the
config class on_error function is called.  From there you can throw an
exception or do whatever error handling you like:  So in the case of date_time
there are 2 steps:
1) define a unique exception type
2) use the simple_exception_policy helper to configure the constrained_value
  struct bad_month : public std::out_of_range
  {
    bad_month() : std::out_of_range(std::string("Month number is out of range
1..12")) {}
  };
  //! Build a policy class for the greg_month_rep
  typedef CV::simple_exception_policy<unsigned short, 1, 12, bad_month>
greg_month_policies;
  //! A constrained range that implements the gregorian_month rules
  typedef CV::constrained_value<greg_month_policies> greg_month_rep;
Now the following code will throw bad_month exception:
  greg_month_rep m(13); //throws bad_month
  greg_month_rep m(0); //throws bad_month
  greg_month_rep m(12); //ok
> you to specify the range that a 
> number can take, e.g.
> 
>    boost::range< int, 1, 12 > month;
> 
> generating an error on an invalid range, e.g.
> 
>    month = 20; // [1] oops!
> 
> Also, is it possible to distinguish between a number literal like 
> the above and a variable assignment, like:
> 
>    month = m; // [2]
Not sure. constrained_value doesn't.
 
> The logic behind this is to provide compile-time checks where 
> possible, so [1] would produce a compile-time error and [2] would be 
> a run-time error. You could also do something like:
> 
>    inline char & string::operator[]( boost::range< int, 5, 10 > i ){ 
> ... }   // ...   string s;   s[ 1 ] = 'a'; // oops!
> 
> and
> 
>    class date
>    {
>       boost::range< int, 1, 12 > month;
>       public:
>          inline date( boost::range< int, 1, 12 > m ): month( m ){}
>    };
Exactly -- and your code isn't littered with all sorts of range checking logic...
Jeff