Take a look at the following code:
#include <cstdio>
#include <set>
using namespace std;
set<int> s;
int main()
{
printf("%d", s.size() > -1);
return 0;
}
Output is 0, but it should be 1 (obviously s.size() = 0 > -1
). Code is tested on ideone and codeforces compilers. Can somebody explain this?
s.size()
returns an unsigned value. When you compare it to signed-1
,-1
is promoted to an unsigned value too and becomesSIZE_MAX
. Thens.size() > SIZE_MAX
yieldsfalse
.Your code is equivalent to this:
b is casted to unsigned and becomes greater than 0 so there is no bug in the compiler.
try this: