473,326 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 4030
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: MiniDisc_2k2 | last post by:
Okay, here's a question about the standard. What does it say about unsigned/signed mismatches in a comparison statement: char a = 3; unsigned char b = 255; if (a<b) Now what's the real...
3
by: Siemel Naran | last post by:
Hi. Is there a way to convert the type signed int to the type unsigned int, char to unsigned char, signed char to unsigned char, and so on for all the fundamental integer types? Something like ...
19
by: Christopher Benson-Manica | last post by:
Given signed char str_a="Hello, world!\n"; unsigned char str_b="Hello, world!\n"; what is the difference, if any, between the following two statements? printf( "%s", str_a ); printf( "%s",...
9
by: dam_fool_2003 | last post by:
For int data type the default range starts from signed to unsigned. If we don't want negative value we can force an unsigned value. The same goes for long also. But I don't understand why we have...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
64
by: ng5000 | last post by:
Hi, What's the point of a signed char? As I see it a char represents a character (not an integer, use an int type e.g. short int if you want an 8 bit number, or one of the new types, uint8 I...
22
by: juanitofoo | last post by:
Hello, I've just switched to gcc 4 and I came across a bunch of warnings that I can't fix. Example: #include <stdio.h> int main() { signed char *p = "Hola";
1
by: nicola | last post by:
Hi, I'm a bit confused about the following: #include <iostream> int main (int argc, char** argv) { std::cout << "Bits of unsigned char: " << std::numeric_limits<unsigned char>::digits <<...
6
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.