Boost logo

Boost :

Subject: Re: [boost] Towards a Warning free code policy proposal
From: Emil Dotchevski (emil_at_[hidden])
Date: 2010-08-29 13:47:29

On Sun, Aug 29, 2010 at 2:50 AM, Mathias Gaunard
<mathias.gaunard_at_[hidden]> 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.

Boost list run by bdawes at, gregod at, cpdaniel at, john at