Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2004-01-03 16:42:48


Michael D. Borghardt wrote:

>I am trying to use the numeric_cast, but I do not understand what is
>happening below. Test 2 does not fail but I expect it to.

>Can someone explain what is happening?
>[snip] // note: the code below is also abridged:

> int a;

> // I expect Test 1 to fail with a loss of range - which it does
> a = 255;
> boost::numeric_cast<signed char>(a);

This fails because a signed char type has a range between -127 and +128 (or
-128 and +127, not sure) and +255 > +128.

> // I expect Test 2 to fail with a loss of range - but it does not
> a = 255;
> numeric_class<unsigned char> uc(a);

An unsigned char has a range between 0 and 255, thus 255 is an acceptable
value for it (whereas 256 is not). This is why the test passes.

> // I expect Test 3 to fail with a loss of range - which it does
> a = -1;
> numeric_class<unsigned char> uc(a);

In this case, the value of a is (assuming int is two bytes by default)
0xFFFF which - in its usigned form - is 65535, which is why this fails.

Hope that helps,
Reece

_________________________________________________________________
Tired of 56k? Get a FREE BT Broadband connection
http://www.msn.co.uk/specials/btbroadband


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk