Boost logo

Boost Users :

Subject: Re: [Boost-users] Problem with iterator_facade
From: Daniel Krügler (dsp_at_[hidden])
Date: 2008-09-17 07:21:00


Robert Dailey 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.

I guess you want:

class PassIterator
                : public boost::iterator_facade<
                        PassIterator,
                        const Pass, // #1
                        boost::forward_traversal_tag
>

...
                const Pass& dereference() const; // #2
...
};

Alternatively you could do this:

class PassIterator
                : public boost::iterator_facade<
                        PassIterator,
                        Pass, // #1
                        boost::forward_traversal_tag,
                         Pass // #2
>

...
                Pass dereference() const; // #3
...
};

Greetings from Bremen,

Daniel Krügler


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