Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2002-02-08 09:03:25


I can't fault most of your reasoning here. If you submit reasonable patches
I'll be happy to incorporate them.


----- Original Message -----
From: "Bjorn Karlsson" <bjorn.karlsson_at_[hidden]>

> When looking at the numeric_cast implementation, I was perplexed at some
> the range checks.
> For example, the test for too large value on a conversion from unsigned to
> signed looks like this:
> template <> struct greater_than_type_max<false, false> {
> template <class X, class Y>
> static inline bool check(X x, Y)

> return static_cast<X>(static_cast<Y>(x)) != x;
> }
> };
> The implementation obviously expects values to wrap if too large, so for a
> conversion to a smaller type, the first static_cast produces a smaller
> if the range was violated. Then, the value is cast back to the original
> type, which would yield a different value than the original.
> First of all, how safe is it to expect that all implementations wrap
> (It's not specified by the Standard, but on the other hand I've never seen
> any other behavior.)

It doesn't depend on wrapping, just on the idea that if you can represent
all values of signed Y as an unsigned X, you can't represent all values of X
as a Y. Of course, it also doesn't work for integers of the same size...

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