
8 Jan
2010
8 Jan
'10
1:14 a.m.
Does the BOOST_FOREACH macro work for empty sequences or must a size check be done explicitly?
list<int> mylist;
if (!mylist.empty()) //<--- necessary? { foreach(int& i, mylist) { // take action } }
The reason I ask is that neither begin() nor end() return valid iterators for empty sequences, but it is my understanding that BOOST_FOREACH needs iterators to work properly.
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.