Boost logo

Boost Users :

Subject: Re: [Boost-users] Strange error while using BOOST_FOREACH
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2012-03-12 00:25:48


> My collection does not have a const iterator defined for it. I suppose that bothers the
> machinery in FOREACH even though I'm not asking for one here?

Let's take a look at the docs. The Foreach docs say [1] that Foreach is built
on top of Boost.Range, and supports types that satisfy the Single Pass Range
Concept.

The Boost.Range iterator docs on the Single Pass Range Concept [2] say that
such ranges have associated iterator and const iterator types, accessible
by boost::range_iterator<X>::type and boost::range_iterator<const X>::type,
respectively.

There are two methods for adapting a type to fit the Single Pass Range
Concept requirement. The most commonly used method [3] is to give your range
type methods named 'begin()' and 'end()' and typedefs named 'iterator' and
'const_iterator'. There is also another method suitable for cases where you
cannot modify the range type [4].

So yes, if you want your range type to work with Boost.Foreach, you should
employ one of those methods to make it compliant to the Single Pass Range
Concept (including having a const iterator).

Regards,
Nate

[1] http://www.boost.org/doc/libs/1_49_0/doc/html/foreach.html
[2] http://www.boost.org/doc/libs/1_49_0/libs/range/doc/html/range/concepts/single_pass_range.html
[3] http://www.boost.org/doc/libs/1_49_0/libs/range/doc/html/range/reference/extending/method_1.html
[4] http://www.boost.org/doc/libs/1_49_0/libs/range/doc/html/range/reference/extending/method_2.html
                                               


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