From: Nick Hounsome on
On 10 Jan, 01:21, Francis Glassborow
<francis.glassbo...(a)btinternet.com> wrote:
> Nick Hounsome wrote:
> > On Jan 8, 1:39 pm, leonleon77 <leonleo...(a)gmail.com> wrote:
>
> > Firstly the type is "unsigned char" not "unsigned very short int" and
> > char is short for character so I would expect a character to be output
> > not an integer.
>
> An interesting assertion. However arithmetic operations on characters
> would be largely meaningless. In both C and C++ the various char types
> are integer types and mostly behave that way unless they are used in a
> context where characters would make sense. IOWs the interpretation of
> the values stored in char types is context dependant.

It's an opinion based on my expectations of the usage of English (or
near English) not an assertion.

Anyway arithmetic operations on characters ARE meaningless:

What is 'a' + 'b'?
Even '0'+1 is not necessarily '1' (and anyway this is arithmetic on
int NOT char)

In fact I think that until relatively recently arithmetic with char
wasn't even possible as chars were always promoted to int first!

Using char for storing small ints is really equivalent to something
like using reinterpret_cast to store ints in doubles - You can do it
(although there is no good reason to try because you can just use
integer types) but when you cout the double it will still come out as
a floating point number:

char c1 = reinterpret_cast<char>(42);
c1 = reinterpret_cast<char>( (reinterpret_cast<int>(c1) + 1 )
cout << c1; // defintely not 43

double d1 = reinterpret_cast<double>(42);
d1 = reinterpret_cast<double>( reinterpret_cast<int>(d1) + 1 );
cout << d1; // defintely not 43

Both should hold the integer representation of 43 but neither will
output 43.

In summary char's are not ints - It just happens that you can
reasonably conveniently stuff ints into them and get them back again.

IMHO one way to make things more regular would be to have
uint_least8_t etc be types rather than typedefs
then you would have a portable way to handle byte oriented data
(unfortunately it cant be uint8_t because of a tiny number of machines
without 8 bit access).


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]