Re: negative number evaluating greater than string.size()
John Harrison wrote:[color=blue]
> "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
> news:c12sm0$kkk @dispatch.conce ntric.net...
>[color=green]
>>Jason wrote:
>>[color=darkred]
>>>Hi, below is example code which demonstrates a problem I have encountered. When passing a number to a function I compare it with a string's size and then take certain actions, unfortunately during the testing of it I discovered that negative numbers were being treated as if they were > 0. I compiled the following on mingw compiler/dev c++/windows xp. If I replace string.size() for a ordinary number it behaves as expected? I
>>>notice string.size() returns size type and not an int but how do I deal with that? Thanks in advance for any help[/color][/color][/color]
<reinserted>
void something(int number) {
if(number < str.size()) { /*...*/ }
}
</>
[color=blue][color=green]
>>GCC sez:
>>
>>signed_probs. cpp: In function `void something(int)' :
>>signed_probs. cpp:17: warning: comparison between signed and unsigned
>>integer expressions
>>
>>Changing line 17 to :
>>
>> if(number < static_cast<int >(str.size()) ) { cout << " hello" << endl; }
>>
>>solves the problem.
>>[/color]
>
>
> Of course this is what the OP should do,[/color]
No, an unnecessary cast is not what the OP should do. The problem is
that a signed "number" is being compared to an unsigned "size." "int"
and "string::size_t ype" are not the same thing; they are not always
comparable. A more reasonable solution would be for "number" to be of
string::size_ty pe in the first place, or of a signed type that knows how
to compare itself with string::size_ty pe.
John Harrison wrote:[color=blue]
> "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
> news:c12sm0$kkk @dispatch.conce ntric.net...
>[color=green]
>>Jason wrote:
>>[color=darkred]
>>>Hi, below is example code which demonstrates a problem I have encountered. When passing a number to a function I compare it with a string's size and then take certain actions, unfortunately during the testing of it I discovered that negative numbers were being treated as if they were > 0. I compiled the following on mingw compiler/dev c++/windows xp. If I replace string.size() for a ordinary number it behaves as expected? I
>>>notice string.size() returns size type and not an int but how do I deal with that? Thanks in advance for any help[/color][/color][/color]
<reinserted>
void something(int number) {
if(number < str.size()) { /*...*/ }
}
</>
[color=blue][color=green]
>>GCC sez:
>>
>>signed_probs. cpp: In function `void something(int)' :
>>signed_probs. cpp:17: warning: comparison between signed and unsigned
>>integer expressions
>>
>>Changing line 17 to :
>>
>> if(number < static_cast<int >(str.size()) ) { cout << " hello" << endl; }
>>
>>solves the problem.
>>[/color]
>
>
> Of course this is what the OP should do,[/color]
No, an unnecessary cast is not what the OP should do. The problem is
that a signed "number" is being compared to an unsigned "size." "int"
and "string::size_t ype" are not the same thing; they are not always
comparable. A more reasonable solution would be for "number" to be of
string::size_ty pe in the first place, or of a signed type that knows how
to compare itself with string::size_ty pe.
Comment