Whenever you write a data type as "int", is it a guaranteed alias for "signed int", or is that defined by the compilation options?
Printable View
Whenever you write a data type as "int", is it a guaranteed alias for "signed int", or is that defined by the compilation options?
I've seen compilers that offer "default char to unsigned" as an option, but I havent seen "int to unsigned".
Yeah.
However, "char" is it's own data type which is different from "unsigned char" and "signed char", so that option defines how "char" should behave. Even if "char" is unsigned, it is not the same data type as "unsigned char".
int, on the other hand, as far as I know, is jus short-hand for signed int. I'm 90% sure this is guaranteed by the standard, but I want to make sure.
3.9.1 lists "int" as one of the five standard signed integer types.
well, you can easily test this
Comeau givesCode:template <class T> struct Test{};
template <> struct Test<char>{};
template <> struct Test<signed char>{};
template <> struct Test<unsigned char>{};
template <> struct Test<int>{};
template <> struct Test<signed int>{};
template <> struct Test<unsigned int>{};
Quote:
Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 8: error: class "Test<int>" has already been defined
template <> struct Test<signed int>{};
^
1 error detected in the compilation of "ComeauTest.c".
It's supported on both my GCC and VS2008. I think the "problem" is that the operator<< on ostream has the same behaviour for "char", "unsigned char" and "signed char", which is to print the character, and not the value.
The easy fix is to just call the unary operator+ first:
operator+ will trigger type promotion, turning unsigned chars into unsigned ints, signed chars into signed ints, and leave everything else un-changed.*Code:unsigned char myChar = ...;
...
out << +myChar;
*Except shorts, if you want to get technical.
Genius, I love it. :)
Absolutely a beautiful little trick that saves a lot of typing! :thumb:
I still think the stream operators should print characters when fed with a char and numbers for an unsigned/signed char. After all the types are separated by the standard. Does anybody know why the compilers (all?) do not support it?