Why can't size_t be directly compared with negative int?

Viewed 4301

One bug in my program is caused by directly comparing size_t with int.

A toy sample code is as follows:

string s = "AB";
cout << (-1 < 8888888+(int)s.size()) << endl;
cout << (-1 < 8888888+s.size()) << endl;

The output is:

1

0

So why can't size_t be directly compared with negative int?

2 Answers
Related