Boost logo

Boost :

Subject: Re: [boost] Towards a Warning free code policy proposal
From: Nevin Liber (nevin_at_[hidden])
Date: 2010-08-30 11:26:41


On 29 August 2010 04:50, Mathias Gaunard <mathias.gaunard_at_[hidden]> wrote:
>   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'

Until C++0x and auto, it can be fairly painful to get size_type of the
container, since you can't get it from the "object" variable itself.
Personally, I go through the pain, but I can certainly understand why
others may not. Library code, such as Boost, should do this for
correctness.

>  - using indexes and size instead of iterators, which have an idiomatic way
> to be traversed

push_back(), for instance, may invalidate iterators with a vector or
deque but will not invalidate indices.

>  - 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.

Even std::sort doesn't work on *any* sequence type. There are usually
other requirements (random access, contiguous space, O(1)
insertion/removal, etc.) that limit the choice of data structures.

-- 
 Nevin Liber  <mailto:nevin_at_[hidden]>  (847) 691-1404

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk