Boost logo

Boost Users :

Subject: Re: [Boost-users] Problem with iterator_facade
From: David Abrahams (dave_at_[hidden])
Date: 2008-09-17 11:35:06


on Tue Sep 16 2008, "Robert Dailey" <rcdailey-AT-gmail.com> wrote:

> Hi,
>
> I have the following iterator_facade class:
>
> class PassIterator
> : public boost::iterator_facade<
> PassIterator,
> Pass,
> boost::forward_traversal_tag
> >
> {
> public:
> PassIterator();
>
> PassIterator( Effect const& effect );
>
> private:
> void increment();
> Pass& dereference() const;
>
> Pass m_pass;
>
> friend class boost::iterator_core_access;
> };
>
> Note how my Pass object is stored by-value in my PassIterator class.
> When I attempt to compile this class, it fails because dereference()
> is const and it is trying to return a reference to a const member.
> However, if I make dereference() non-const, it still fails because the
> base class is calling into PassIterator::dereference() through a const
> member function as well, so dereference() *must* be const in order to
> be callable by boost.
>
> How can I overcome this issue? const_cast? mutable? Thanks.

Mutable, it seems to me. Dereference must be a non-mutating operation
on the iterator itself. The fact that the referenced object happens to
be stored inside the iterator notwithstanding, it is not logically a
part of the iterator's value.

-- 
Dave Abrahams
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