
On 28/08/2010 22:11, Mika Heiskanen wrote:
On Sat, 2010-08-28 at 22:17 +0300, Robert Ramey wrote:
d) some were too hard to fix/eliminate comparison between signed/unsigned - I just left them because I didn't know how to fix them
Comparing signed and unsigned integers can be a serious issue, since that means you might not deal with negative cases properly, which is why there are warnings.
Am I missing some complication I'm not aware of?
In my experience this warning is just about always the result of using int for variables which should be of type size_t, as in
for(int i=0; i<object.size(); i++)
I never understood why some people even write code like this. There are several problems with this: - using int, even though int has no direct relation with the size type of 'object' - using indexes and size instead of iterators, which have an idiomatic way to be traversed - making the code rely on a particular data structure, instead of allowing it to work with any sequence type, allowing the code to be more easily changed.
If I cannot redefine all the types to unsigned, something like this usually fixes the problem:
if ( size>= 0&& static_cast<size_t>(size)< object.size() )
size_t(size) instead of static_cast<size_t>(size) should work just fine. No need for any cast.