I would like to have your advice regarding the usage of BOOST_FOREACH
.
I have read around it is not really recommended in terms of performance being a very heavy header.
Moreover, it forces the use of "break" and "continue" statements since you can't really have an exit condition driven by a boolean and I've always been told that "break" and "continue" should be avoided when possible.
Of course, the advantages are that your are not dealing directly with iterators which ease the task of iterating through a container.
What do you think about it?
Do you think that if used it should be adopted systematically to guarantee homogeneity in a project or its use is recommended only under certain circumstances?
I would say C++ range based loops supercede it. This is an equivalent of this BOOST_FOREACH example:
std::string hello( "Hello, world!" );
for (auto c : hello)
{
std::cout << c;
}
I never found I needed to use it in ++03.
Note when using the range based loop over containers with expensive to copy elements, or in a generic context, it is best to use const&
to those elements:
SomeContainerType<SomeType> v = ....;
for (const auto& elem : v)
{
std::cout << elem << " ";
}
Similarly, if you need to modify the elements of the container, then use a non-const & (auto& elem : v
).