
Is there a boost library that allows 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] 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 ){} }; Regards, Reece _________________________________________________________________ Sign-up for a FREE BT Broadband connection today! http://www.msn.co.uk/specials/btbroadband

Hi, Reece Dunn wrote:
Is there a boost library that allows you to specify the range that a number can take, e.g.
Few weeks ago there was a discussion here on this exact topic. The initiating post was: http://lists.boost.org/MailArchives/boost/msg62676.php and my response was: http://lists.boost.org/MailArchives/boost/msg62678.php I do not see any other post in this thread, but it took only two or three more posts until the thread died due to the lack of interest from other participants. In short, there is no such library in Boost. You can, however, try to glue toghether the pieces presented in the posts above to achieve your goal. Good luck, -- Maciej Sobczak : http://www.msobczak.com/ Programming : http://www.msobczak.com/prog/

On Mon, 10 May 2004 16:23:55 +0200, Maciej Sobczak wrote
Reece Dunn wrote:
Is there a boost library that allows you to specify the range that a number can take, e.g.
Few weeks ago there was a discussion here on this exact topic.
...
I do not see any other post in this thread, but it took only two or three more posts until the thread died due to the lack of interest from other participants.
Hmm, sorry I missed this thread -- must have been busy ;-)
In short, there is no such library in Boost. You can, however, try to glue toghether the pieces presented in the posts above to achieve your goal.
I've posted a pointer to what I use in date_time for this purpose. It doesn't support the static assignment check, but to me that is secondary. The majority case is the dynamic check -- of course enhancements are always welcome. http://lists.boost.org/MailArchives/boost/msg64485.php I wouldn't mind having this capability pulled out into it's own mini-lib if we have multiple uses... Jeff

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. http://www.crystalclearsoftware.com/libraries/gdtl/gdtl_ref_guide/classboost... 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
participants (3)
-
Jeff Garland
-
Maciej Sobczak
-
Reece Dunn