I have code that does something like this:
Code:
void func( char* str, unsigned long len )
{
char* pos = str + someNumber;
...
while ( pos - str < len )
{
...
}
}
This produces a signed/unsigned mismatch for the < comparison, so to get rid of that warning, I need to cast one side to the other.
I'm not sure if it's better in this case to go with:
Code:
assert( len <= LONG_MAX );
while ( pos - str < static_cast<long>(len) )
or
Code:
while ( static_cast<unsigned long>(pos - str) < len )
{
assert( (pos - str) >= 0 );
...
}
It's mainly the result of char* - char* that I'm wondering about. You can't have a negative pointer value, so I'm not sure why the compiler thinks (pos - str) is signed?