I have the following C++ code:
return lineNum >= startLineNum && lineNum <= startLineNum + lines.size() - 1; Here, lineNum is an int, startLineNum is an int, lines is a std::vector<std::string>, and lines.size() is of type size_t.
When lineNum is 2, startLineNum is 0, and lines.size() is 0, the code returns true even though false was expected. These values were the values displayed in the debugger.
Even after adding parentheses where possible:
return ((lineNum >= startLineNum) && (lineNum <= (startLineNum + lines.size() - 1))); the code still incorrectly returns true.
When I refactor the code into this form:
int start = startLineNum; int end = startLineNum + lines.size() - 1; return lineNum >= start && lineNum <= end; it now returns false as expected.
What is going on here? I have never come across this kind of strangeness before.