Joel F Real men use unique_ptr | bon bref, je mets au point une classe template de gestion des intervals :
Code :
- /*!
- *********************************************************************
- * @brief Range error exception class.
- *
- * The Error class is solely created to be thrown by the
- * Range class when a range error is caught.
- *
- * <code>
- * try
- * {
- * Range<int,0,5> x = 3; // OK
- * Range<int,0,5> y = 8; // KO
- * }
- * catch( Range::Error )
- * {
- * cout << " A Range error has been caught !!" << endl;
- * }
- * </code>
- *
- *
- *********************************************************************
- */
- class RangeError {};
- /*!
- *********************************************************************
- * @brief Value range template.
- *
- * The Range template allow users to create a type representing a
- * contiguous range of value. It could be transparently used to specify
- * function prototypes :
- *
- * <code>
- * double arcSinus( Range<double,-1.0,1.0> val );
- * </code>
- *
- * The template parameters describes the types of the range data,
- * the lower bound and the higher bound of the Range.
- *
- * If val is out of the [-1,1[ bound, a Error exception is
- * thrown and could be handled. Note that the range is open on the
- * upper end. When calling arcSinus, you use the normal way to call it :
- *
- * <code>
- * double r = arcSinus(0.25); // valid
- *
- * r = arcSinus( 2.5); // throws Error.
- * </code>
- *
- * Note also that we must initialize any Range object when we create
- * it hence there is no default constructor for it.
- *
- * <code>
- * Range<char,'a','z'> letter; // KO
- *
- * Range<char,'a','z'> letter = 'e'; // OK
- * </code>
- *
- *********************************************************************
- */
- template< typename T, T low, T high > class Range
- {
- public:
- Range( T val ) { Assert<RangeError>((val>=low) && (val<high)); value = val; }
- Range operator=( T val ) { return *this=Range(val); }
- operator T() { return value; }
- private:
- T value;
- };
|
La fonction Assert<> est :
Code :
- template<class E, class A> Assert( A assertion )
- {
- if( !assertion ) throw E();
- }
|
Tout a l'air OK, suaf que quand je compile la chose :
Code :
- try
- {
- Range<int,0,15> x = 2;
- cout << "x : " << x << endl;
- Range<int,0,5> y = 8;
- cout << "y : " << y << endl; // Jamais executé ...
- }
- catch( RangeError )
- {
- cout << "Erreur de range !!" << endl;
- }
|
x passe et au lieu de prendre la branche catch a la declarration de y, c'est une Unhandled Exception de KERNEL32 qui est levée ...
Moi pas comprendre ... Message édité par Joel F le 30-12-2002 à 14:27:51
|