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$1_at_sea.gmane.org...
> When I followed iy throught the debugger the min and max values were
> SCHAR_MIN
> 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
preserved).

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
have.
It also contains a "replacement" for numeric_cast<> to fix precisely this
sort of problems.

Check it out here

http://personales.ciudad.com.ar/fernando_cacciola/numeric_conversions.zip

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 acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk