This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: result type of an operation betweem signed and unsigned, c++
- From: Andrew Haley <aph-gcc at littlepinkcloud dot COM>
- To: "Dima Sorkin" <dima dot sorkin at gmail dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Tue, 24 Apr 2007 14:41:16 +0100
- Subject: Re: result type of an operation betweem signed and unsigned, c++
- References: <e40293600704240622m60cfa389q97695f7430b849d8@mail.gmail.com>
Dima Sorkin writes:
> Hi.
> The following surprised me a lot: I thought that a subtraction of
> a 'signed' and 'unsigned' integers has the type "signed",
> but it seems the opposite:
>
> -------------------------------------------------------
> // u.cc
> #include<iostream>
>
> int main(){
> int i(0); unsigned u(1);
>
> bool isPos( i-u > 0 ); int res(i-u);
>
> std::cout << i-u << ' ' << isPos << ' ' << res <<'\n';
> }
>
> > g++ -ansi -pedantic -Wall u.cc
> > ./a.out
> 4294967295 1 -1 <----------------- See the first two results
> ----------------------------------------------------------
>
> 1) Is type of 'signed-unsigned' defined in c++, or it is up to compiler ?
> (is the above behaviour a failure ?)
No, it's not. See ISO/IEC 14882:1998(E) Section 5, Expressions,
Para. 9.
> 2) Is there some way to convert a type of an expression into
> _readable_ string ("typeof"), to debug such things ?
>
> 2b) Is it true that any legal expression has a type,
Yes. All will become clear if you read the first part of Section 5.
Andrew.