|
Boost Users : |
From: Keith MacDonald (boost_at_[hidden])
Date: 2004-01-03 12:03:28
Hi,
I'm stuck trying to use iterator_adaptor from the CVS head. I simply want
to derive a non-const iterator from a const one derived from
iterator_facade, as shown in the code below. When I compile it with VC7.1,
the line marked "/* *ERROR* */" generates one of those impenetratable error
messages that starts with:
cannot convert from 'const Node' to
'boost::iterator_facade<Derived,Value,CategoryOrTraversal,Reference,Differen
ce>::reference'
If I delete that line, so the default is used, dereferencing a non-const
iterator returns a const Node. This seems such a simple requirement that
there must be an easy way to do it. What am I doing wrong?
Thanks,
Keith MacDonald
/* ========================= Code Begins ========================= */
#include <boost/iterator/iterator_facade.hpp>
#include <boost/iterator/iterator_adaptor.hpp>
class Node
{
// ...
// Iteration:
public:
class iterator;
class const_iterator;
// Read-only, bidirectional iterator:
typedef boost::iterator_facade<
const_iterator,
const Node,
std::bidirectional_iterator_tag
> boost_const_iterator;
class const_iterator
: public boost_const_iterator
{
public:
const_iterator()
{}
const_iterator(const iterator& it)
: m_pNode(it.base().m_pNode) {}
explicit const_iterator(const Node* pNode)
: m_pNode(pNode) {}
friend class iterator;
friend class boost::iterator_core_access;
private:
// Interface required by iterator_facade:
reference dereference() const
{ return *m_pNode; }
void increment()
{ m_pNode = m_pNode->next(); }
void decrement()
{ m_pNode = m_pNode->prev(); }
bool equal(const_iterator it) const
{ return m_pNode == it.m_pNode; }
protected:
const Node* m_pNode;
};
// Read-write, bidirectional iterator,
// adapted from const_iterator:
typedef boost::iterator_adaptor<
iterator,
const_iterator,
Node /* *ERROR* */
> iterator_base;
class iterator
: public iterator_base
{
public:
iterator() {}
explicit iterator(Node* pNode)
: iterator_base(const_iterator(pNode)) {}
};
const_iterator begin() const
{
return const_iterator(m_pChild);
}
const_iterator end() const
{
return const_iterator(NULL);
}
iterator begin()
{
return iterator(m_pChild);
}
iterator end()
{
return iterator(NULL);
}
private:
Node* m_pChild;
Node* m_pPrev;
Node* m_pNext;
};
/* ========================= Code Ends ========================= */
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