[cfe-dev] confusion with character types

Jochen Wilhelmy j.wilhelmy at arcor.de
Thu Oct 14 06:04:21 PDT 2010


Hi!

These thoughts I'd like to share with you about character types (char, 
signed char, unsigned char).
At first I think it's an useful thing that modern languages such as java 
have separate types for
characters and small integers (e.g. char, byte, short).
In c++ we have the following problem:
char a = '0';
unsigned char b = 48;
signed char c = 48;
std::stringstream s; s << a << b << c;
the result is "000".

Now I have "discovered" that it is possible to overload for all three 
char types:
void foo(char);
void foo(unsigned char);
void foo(signed char);

Therefore c++ qualifies as a modern language ;-)
because we can distinguish between characters and small integer types.
It is possible to implement a stream like this
MyStringStream s; s << a << b << c;
the result is "04848".
Of course usually we would use int8_t and uint8_t instead of signed char 
and unsigned char.

So my question: Is there a paricular reason why the standard stream 
interprets signed char and
unsigned char as characters instead of numbers?

Another question: why uses OpenCL char and uchar as 8 bit number types? 
Would it be not
better to use e.g. byte and ubyte as 8 bit number types? maybe one time 
OpenCL gets used to
process strings and then we would need a separate char again. Since 
OpenCL is a c dialect
it soult be possible to typedef signed char to byte and unsigned char to 
ubyte.
This should be proposed to the Khronos Group but I don't know how to do 
it and I doubt
they will listen to me ;-)

-Jochen




More information about the cfe-dev mailing list