Re: signed char
* Marcin Kalicinski:
Are 3 types: signed char, char and unsigned char distinct? My compiler is
treating char as signed char (i.e. it has sign, and range from -128 to 127),
but the following code does not call f<char> as I would expect:
template<class T> void f(T t)
{
}
template<> void f<char>(char t)
{
}
int main()
{
signed char ch = 0;
f(ch); // <-- this calls the unspecialized version
}
The types char, unsigned char and signed char are distinct types wrt.
function overloading and templates.
Do I have to provide specializations for unqualified char and both signed
and unsigned chars?
Depends whether you want to treat them differently or not.
They are the same size, and except for overload resolution, template
specialization and the result of typeid, char is the same as either
signed char or unsigned char, what I call its /underlying type/, --
which one it is depends on the compiler and the compiler settings. The
standard explains this by way of the value sets. That the set of values
for plain char is the same as the set of values for either signed char
or unsigned char, depending on "the implementation", i.e. the compiler.
> How about ints, shorts and longs?
The situation for type char is not replicated for any other type, not
even wchar_t. However, also wchar_t has an underlying type, including
signedness that depends on the compiler, and in fact this is where the
standard uses the term underlying type. The bug (heh heh, Freudian slip
of the keyboard, I meant to write "big") difference from char is that
for purposes of type declaration there's no such beast as signed wchar_t
or unsigned wchar_t.
That's just to make life interesting for programmers, of course, or
perhaps the committe members thought, consistency is way overrated.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?