result type of an operation betweem signed and unsigned, c++

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 ?)

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, or type
    of some expressions may depend on context (i.e. type of
    the lvalue they are assigned to, I know at least one case -
    with function pointers, but is there more simple examples) ?

Thank you, regards
Dima.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux