-
October 25th, 2010, 03:56 AM
#1
Sign of int
Whenever you write a data type as "int", is it a guaranteed alias for "signed int", or is that defined by the compilation options?
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
October 25th, 2010, 04:12 AM
#2
Re: Sign of int
I've seen compilers that offer "default char to unsigned" as an option, but I havent seen "int to unsigned".
your humble savant
-
October 25th, 2010, 05:04 AM
#3
Re: Sign of int
Originally Posted by TheRogue
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.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
October 25th, 2010, 05:10 AM
#4
Re: Sign of int
3.9.1 lists "int" as one of the five standard signed integer types.
your humble savant
-
October 25th, 2010, 05:11 AM
#5
Re: Sign of int
Originally Posted by monarch_dodra
I'm 90% sure this is guaranteed by the standard, but I want to make sure.
well, you can easily test this
Code:
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>{};
Comeau gives
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".
-
October 25th, 2010, 07:13 AM
#6
Re: Sign of int
Originally Posted by superbonzo
well, you can easily test this
Testing the specific implementation of a specific compiler does not test what the standard says.
-
October 25th, 2010, 07:54 AM
#7
Re: Sign of int
Originally Posted by TheRogue
3.9.1 lists "int" as one of the five standard signed integer types.
Indeed, that seems to answer my question. Thanks.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
October 25th, 2010, 03:00 PM
#8
Re: Sign of int
Originally Posted by monarch_dodra
However, "char" is it's own data type which is different from "unsigned char" and "signed char"
I wonder when that is going to be fully supported? It would sure be wonderful to get numbers printed even when forgetting to cast when using << on unsigned chars...
-
October 26th, 2010, 01:33 AM
#9
Re: Sign of int
Originally Posted by S_M_A
I wonder when that is going to be fully supported? It would sure be wonderful to get numbers printed even when forgetting to cast when using << on unsigned chars...
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:
Code:
unsigned char myChar = ...;
...
out << +myChar;
operator+ will trigger type promotion, turning unsigned chars into unsigned ints, signed chars into signed ints, and leave everything else un-changed.*
*Except shorts, if you want to get technical.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
October 26th, 2010, 04:42 AM
#10
Re: Sign of int
Genius, I love it.
-
October 26th, 2010, 01:17 PM
#11
Re: Sign of int
Absolutely a beautiful little trick that saves a lot of typing!
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|