
"Rob Stewart" <stewart@sig.com> wrote in message news:<200404211327.i3LDRov09314@lawrencewelk.systems.susq.com>...
From: "Dill, John" <john-dill@uiowa.edu>
"Rob Stewart" <stewart@sig.com> wrote in message news:<200404202049.i3KKnwe25029@lawrencewelk.systems.susq.com>...
From: "Dill, John" <john-dill@uiowa.edu>
"Rob Stewart" <stewart@sig.com> wrote in message news:<200404201905.i3KJ55e08992@lawrencewelk.systems.susq.com>...
From: "Dill, John" <john-dill@uiowa.edu>
I am wondering about the use-case of numeric_cast in this sample.
unsigned char uchar_max = std::numeric_limits<unsigned char>::max(); char schar_value = 0;
try { schar_value = boost::numeric_cast<char>( uchar_max ); } catch ( boost::bad_numeric_cast ) { std::cout << "Overflow occurred..." << std::endl; }
When I execute this sample, I don't get the exception. What's the background on this behavior? I'm using gcc 3.3.1.
Your char is unsigned.
I'd like to know why conceptually the exception isn't thrown.
As I said, in too few words, is that your "char" is unsigned, so "char" and "unsigned char" are essentially the same type. Technically, they are distinct types (for overloading purposes, for example), but they are both unsigned and the same size, so:
assert(std::numeric_limits<char>::max() == std::numeric_limits<unsigned char>::max());
on your system.
Actually that is not the case. The max defined for unsigned char is 255, while for char is 127. Actually, char is supposedly equivalent to signed char, not to unsigned char.
Based upon the evidence you presented, I concluded that your char is equivalent to unsigned char. On what basis do you conclude that your char is equivalent to signed char? Did you check std::numeric_limits<char>::is_signed?
Yes. Both char and signed char have identical is_signed and max, min. Unsigned char min, max and is_signed is different. Does the standard say that char can be either signed char or unsigned char, or is that just an implementation issue? Best, John

Dill, John wrote:
Does the standard say that char can be either signed char or unsigned char, or is that just an implementation issue?
Best, John
char can be either signed or unsigned. However, it is guaranteed to be a type distinct from either signed char or unsigned char. -- Eric Niebler Boost Consulting www.boost-consulting.com

From: "Dill, John" <john-dill@uiowa.edu>
Does the standard say that char can be either signed char or unsigned char, or is that just an implementation issue?
From 3.9.1/1:
In any particular implementation, a plain char object can take on either the same values as a signed char or an unsigned char; which one is implementation defined. -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;
participants (3)
-
Dill, John
-
Eric Niebler
-
Rob Stewart