Boost logo

Boost Users :

Subject: Re: [Boost-users] [foreach] BOOST_FOREACH question...
From: Eric Niebler (eric_at_[hidden])
Date: 2010-01-07 22:11:25


On 1/8/2010 1:40 PM, barcaroller wrote:
>
> "Dmitry Vinogradov"<sraider_at_[hidden]> wrote:
>
>> To be short: it's not necessary.
>>
>> BTW, begin() and end() for an empty list are valid. You can't dereference
>> them, but they are valid. So one can use them for comparison. And
>> BOOST_FOREACH does.
>
>
> I've had problems with standard 'for' loops when using empty containers:
>
> for (i = mylist.begin() ; i != mylist.end() ; i++)

That's perfectly OK.

> By problems I mean core-dumps, memory leaks, etc. These problems went away
> when I used a size check:
>
> if (!mylist.empty())
> for (i = mylist.begin() ; i != mylist.end() ; i++)
>
> This leads me to believe that begin() and end() are not valid, or is there
> another way to explain this?

I suggest you look for the problem elsewhere. list::begin and list::end
always return valid iterators. Comparing them is fine -- if the list is
empty, the begin and end iterators will compare equal.

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net