
On Sun, Aug 29, 2010 at 2:50 AM, Mathias Gaunard <mathias.gaunard@ens-lyon.org> wrote:
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.
This is besides the point.
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.
T(x) is even more dangerous than static_cast because in certain situations it is equivalent to a C-style cast. The safest thing to do is to let the carefully designed implicit conversion system of the language take care of things. At most, I'd add assert(size>=0) but that's often an overkill. The best way to avoid this warning in this case is to compare with != instead of <. I like != better anyway because it is less defensive, it breaks easier if I mess up the loop. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode