3

Why this (char is signed on my implementation):

cout << std::is_same< char,signed char>::value;

outputs false?

taskinoor
  • 45,586
  • 12
  • 116
  • 142
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194

5 Answers5

6

The three types were introduced at different times.

From the C99 Rational:

Three types of char are specified:
signed, plain, and unsigned. A plain char may be represented as either signed or unsigned depending upon the implementation, as in prior practice. The type signed char was introduced in C89 to make available a one-byte signed integer type on those systems which implement plain char as unsigned char.

They have to stay separate types in C++, to allow overloading on char to be portable.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
4

In case you are using Visual Studio, see here: http://msdn.microsoft.com/en-us/library/cc953fe1.aspx

The C++ compiler treats variables of type char, signed char, and unsigned char as having different types. Variables of type char are promoted to int as if they are type signed char by default, unless the /J compilation option is used. In this case they are treated as type unsigned char and are promoted to int without sign extension.

[Edit] Straight from the ISO C++0x Standard, paragraph 3.9.1 (page 71, http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3092.pdf):

Characters can be explicitly declared unsigned or signed. Plain char, signed char, and unsigned char are three distinct types.

AVH
  • 11,349
  • 4
  • 34
  • 43
2

char, signed char and unsigned char are three distinct types, even if char is interpreted in the same way as signed char is interpreted by your compiler.

§3.9.1/1 from the C++ Standard says

Plain char, signed char, and unsigned char are three distinct types.

In other words, dont think of char as short-form of signed char, because it's not.

Just to emphasize how types could be different despite their bit interpretation being same, consider these two structs:

struct A
{
   int i;
};

struct B
{
   int i;
};

Are they same? Of course not. Exactly in the same way, char and signed char are distinct types.

Try this:

 cout << std::is_same<A,B>::value;
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • I don't think this analogy is relevant. After all, `signed int` and `int` are the same in C++, and in some other languages your structs will be same type (see structural typing). – atzz Apr 06 '11 at 12:36
  • @atzz: The Standard itself says `int` has to be `signed int`,. that implies `int` is a short-form of `signed int`. But in case of `char` and `signed char`, the Standard clearly says they are distinct types! – Nawaz Apr 06 '11 at 12:40
  • 1
    @atzz: a comparison with another language is kinda moot when discussing the intricacies of *one* particular language (by the way, LLVM, which is the backend used by Clang, is using structural typing :p) – Matthieu M. Apr 06 '11 at 12:58
  • @Matthieu M - right. And in the same comment I noted that analogy within C++ doesn't work, and explained why. – atzz Apr 06 '11 at 13:06
  • @Nawaz - I agree. The types are distinct because the standard says so. But the analogy with structs is irrelevant. BTW, I have nothing against the revised version of your answer. – atzz Apr 06 '11 at 13:07
  • 1
    @atzz: Not entirely irrelevant. I actually emphasizes the fact that type could be different, even if the bit *interpretation* are same! – Nawaz Apr 06 '11 at 13:09
  • 1
    @atzz: no, you didn't. Your situation is fundamentally different that the one exposed by Nawaz. Two classes are different in C++. `int` and `signed int` are the same type in C++. There is no parallel between the two. `unsigned int` and `int` are different too, yet extreme structural typing would also assimilate them. – Matthieu M. Apr 06 '11 at 13:37
  • @Matthieu M - My point is that analogy with structs as applied to `char` must in the same way apply to `int`, and it obviously doesn't work for ints. A reference to structural typing served to note that these structs per se do not necessarily have to be distinct types. Note that I was commenting the original version of Nawaz's answer, which offered the analogy as the sole explanation of why `char`, `signed char` and `unsigned char` are all distinct. – atzz Apr 06 '11 at 13:46
  • @atzz: You said : *My point is that analogy with structs as applied to char must in the same way apply to int, and it obviously doesn't work for ints*.. but you're ignoring the fact that the Standard itself says `int` and `signed int` are same. **Also, my struct example was not to prove that they are different, rather to emphasize the fact the bit-interpretation has nothing to do with the types being same or different. `signed char` and `char` might have same interpretation by a particular compiler, but they're are distinct types!** – Nawaz Apr 06 '11 at 13:54
  • @Nawaz - no need to shout. Your original answer didn't mention the standard at all. It just showed the struct analogy in lieu of explanation, which struck me as not really relevant (as the types are distinct *only* because the standard says so), which is why I left a comment. I can't imagine why you find that comment offensive, but if you like to look at life this way, it's your business entirely. Also, as I said already, I have nothing against your revised answer (rev. 3). – atzz Apr 06 '11 at 14:17
  • @atzz: So you saw the complete answer now? And no, I didn't find that comment offsensive. I think there is some misunderstanding. – Nawaz Apr 06 '11 at 14:22
  • @Nawaz - yes, I saw the final version of your answer by the time when I left my second comment, as I noted there. I guess this... discussion is an inherent consequence of SO's editable nature. – atzz Apr 06 '11 at 14:58
  • @atzz: yes, the fact that the comments are not intrinsically tied to a version of the answer make for strange discussions. I agree that if the two structs were all there was, the answer was lacking. On the other hand, I am glad it's editable, as it allows to improve the answers :) – Matthieu M. Apr 06 '11 at 15:18
  • @Matthieu M - I too think that advantages of editing outweigh drawbacks. Usually I try to keep in mind that answers and comments might pertain to a different version of text when responding, but I haven't perfected this skill, yet :) – atzz Apr 06 '11 at 15:26
  • @atzz: me neither, didn't even occurred to me, in fact, that you could have been commenting a different version. – Matthieu M. Apr 06 '11 at 15:27
  • @atzz and @Matthieu: By the way, my first comment itself clarified what the Standard says regarding this topic, but it seems *atzz* was focusing more on my unedited answer than the edited one and my comment. :D – Nawaz Apr 06 '11 at 15:31
1

C++ Standard (quoting Working Draft №3225, 2010-11-27)

3.9.1 Fundamental types

Plain char, signed char, and unsigned char are three distinct types.

atzz
  • 17,507
  • 3
  • 35
  • 35
0

That depends on the implementation, but if I remember correctly I read somewhere that these two should be different, in order to differentiate c type strings from a 8-bit signed number.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • No, it doesn't depend on implementation. At least if we are talking standard-conforming implementations. – atzz Apr 06 '11 at 12:38