Boost logo

Boost :

From: Fernando Cacciola (fernando_cacciola_at_[hidden])
Date: 2004-01-05 20:18:16

"Michael D. Borghardt" <michael_at_[hidden]> escribió en el
mensaje news:bt96p8$17i$
> When I followed iy throught the debugger the min and max values were
> and SCHAR_MAX which are -128 and 127 respectively.
> >
> > What is the representation range of a signed char on your platform?
> >
> > --
> struct greater_than_type_max<false, false>
> {
> // What does the standard say about this? I think it's right, and it
> // will work with every compiler I know of.
> template <class X, class Y>
> static inline bool check(X x, Y)
> { return static_cast<X>(static_cast<Y>(x)) != x; }
> };
> Here is converts 255 to -1 and then converts -1 to 255 and then compares
> to 255.
Which is the expected behaviour for: unsigned char -> signed char ->
unsigned char again.
Signed and unsigned types share the same value representation, which means
that when you convert between them (staying with the same base type, char in
this case), the compiler does actually nothing (the bit patterns are

IOWs, the above check cannot handle your case.

Just recently (very recently), the Numeric Conversions Library was accepted
into boost.
It contains a "converter" class which can detect the out-of-range case you
It also contains a "replacement" for numeric_cast<> to fix precisely this
sort of problems.

Check it out here

WARNING: The library was accepted just a few days ago, and as usual, the
review process raised a number of issues which will require a few changes to
some of the finer functionality of the library.
Stick to the basic (such as the improved numeric_cast<>) to avoid
incompatibilties with the final release version.

Best regards,

Fernando Cacciola

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