signed char

  • Thread starter Marcin Kalicinski
  • Start date
M

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
}

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

Marcin
 
P

Pierre Senellart

"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.
 
J

Jonathan Mcdougall

Marcin said:
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
 
O

osmium

Marcin Kalicinski said:
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.
 
A

Alf P. Steinbach

* 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.
 
P

Pete Becker

osmium said:
:




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.
 
P

Pete Becker

Alf said:
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."
 
O

osmium

Pete Becker said:
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.
 
O

Old Wolf

osmium said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top