469,643 Members | 1,386 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,643 developers. It's quick & easy.

signed char

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
}

Do I have to provide specializations for unqualified char and both signed
and unsigned chars? How about ints, shorts and longs?

Marcin
May 2 '06 #1
8 3522
"Marcin Kalicinski" ,comp.lang.c++:
Do I have to provide specializations for unqualified char and both signed
and unsigned chars?
Yes. char, signed char and unsigned char are three disctint types. Whether a
char can contain negative value is implementation dependent.
How about ints, shorts and longs?


signed (int), signed short (int) and signed long (int) are respectively
the same as int, short int and long int.
May 2 '06 #2
Marcin Kalicinski wrote:
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:


char is an exception in C++. char, signed char and unsigned char are
three distinct types. It does not matter whether a plain 'char' is
actually signed or unsigned.

As for other integral types, signed T and T are equivalent.
Jonathan

May 2 '06 #3
"Marcin Kalicinski" writes:
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:


No. char is the same as unsigned char or signed char, the user (coder) just
doesn't know which one he is going to get when he writes char. It is a
good thing to resolve this early on when you get a new compiler. It may
have a means (in the IDE if it has one) to change the default interpretation
of char.

char means you don't really care what you get.
May 2 '06 #4
* 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?
May 2 '06 #5
osmium wrote:
"Marcin Kalicinski" writes:

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:

No. char is the same as unsigned char or signed char, the user (coder) just
doesn't know which one he is going to get when he writes char.


char is a distinct type from unsigned char and from signed char, but it
has the same representation and properties as one or the other. So you
do have to provide three overloads if you want to cover all three char
types.
It is a
good thing to resolve this early on when you get a new compiler. It may
have a means (in the IDE if it has one) to change the default interpretation
of char.

char means you don't really care what you get.


char means you want to deal with native characters. When you use char as
an arithmetic type, choose either signed char or unsigned char as
appropriate. If you use plain char as an arithmetic type it's because,
indeed, you don't really care what you get.

--

Pete Becker
Roundhouse Consulting, Ltd.
May 2 '06 #6
Alf P. Steinbach wrote:

That's just to make life interesting for programmers, of course, or
perhaps the committe members thought, consistency is way overrated.


They agree with Emerson: "A foolish consistency is the hobgoblin of
little minds."

--

Pete Becker
Roundhouse Consulting, Ltd.
May 2 '06 #7
"Pete Becker" writes:
osmium wrote:
"Marcin Kalicinski" writes:

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:

No. char is the same as unsigned char or signed char, the user (coder)
just doesn't know which one he is going to get when he writes char.


char is a distinct type from unsigned char and from signed char, but it
has the same representation and properties as one or the other. So you do
have to provide three overloads if you want to cover all three char types.


That's a good point. The OP clearly knew there were three names, he even
listed them, so I said, basically, there are three names for two things.
He just asked the wrong question to get the right answer out of me.
May 2 '06 #8
osmium wrote:
That's a good point. The OP clearly knew there were three names, he even
listed them, so I said, basically, there are three names for two things.
He just asked the wrong question to get the right answer out of me.


There are three different types, two of which have identical
properties.

If a template has a specialization for "char", then neither
"signed char" nor "unsigned char" will match that.

Your response looked like you were saying that there are only two
types, and one of them has two names.

May 3 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by MiniDisc_2k2 | last post: by
3 posts views Thread by Siemel Naran | last post: by
19 posts views Thread by Christopher Benson-Manica | last post: by
9 posts views Thread by dam_fool_2003 | last post: by
64 posts views Thread by ng5000 | last post: by
22 posts views Thread by juanitofoo | last post: by
1 post views Thread by nicola | last post: by
6 posts views Thread by Kislay | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.