Boost logo

Boost :

From: Dave Harris (brangdon_at_[hidden])
Date: 2005-05-05 07:27:38

In-Reply-To: <4277C3E9.60407_at_[hidden]>
eric_at_[hidden] (Eric Niebler) wrote (abridged):
> Is do/while an algorithm?

    for (iterator i = c.begin(); i != c.end(); ++i)

is an algorithm. Do/while would be if it included the code to step through
a container.

> You probably don't think of it that way. It's certainly not an
> algorithm in the STL sense like std::for_each is.

Again, a for-loop is.

> The most effective way to think of BOOST_FOREACH (in my experience)
> is as a new looping construct -- a foreach keyword.

It's not a keyword. It's a macro which encapsulates an algorithm - roughly
the same algorithm as std::for_each.

> To encourage people to think of it as an algorithm in the STL sense
> would be a mistake. It is range-based, not iterator-based.

An algorithm does not need to be iterator-based to be an algorithm.

> You can break and continue and return from the loop body. You can't
> do any of those things from an STL algorithm.

That's because the STL algorithms encapsulate differently, not because
they are not algorithms.

In any case, the question of whether FOREACH is an algorithm is a red
herring, because the use of underlines as word separators does not just
apply to algorithms. It also applies to macros (eg "BOOST_STATIC_ASSERT")
and keywords (eg "static_cast"). Although there are some C++ keywords that
don't have underlines, eg "sizeof", these are mostly inherited from C. The
modern trend is for underlines unless there is a compelling reason
otherwise. You've not offered a compelling reason.

It sounds like you are mimicking the "foreach" of some other language. I
think a C++ library should use C++ conventions, not the conventions of
whatever it is you are copying. Consistency is important. Also, program
text is easier to read if words don't all run together.

-- Dave Harris, Nottingham, UK.

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