Boost logo

Boost Users :

From: Keith MacDonald (boost_at_[hidden])
Date: 2004-01-03 12:03:28


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

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?

Keith MacDonald

/* ========================= Code Begins ========================= */
#include <boost/iterator/iterator_facade.hpp>
#include <boost/iterator/iterator_adaptor.hpp>

class Node
 // ...

 // Iteration:
 class iterator;
 class const_iterator;

 // Read-only, bidirectional iterator:
 typedef boost::iterator_facade<
   const Node,
> boost_const_iterator;

 class const_iterator
  : public boost_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;

  // 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; }

  const Node* m_pNode;

 // Read-write, bidirectional iterator,
 // adapted from const_iterator:
 typedef boost::iterator_adaptor<
   Node /* *ERROR* */
> iterator_base;

 class iterator
  : public iterator_base
  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);

 Node* m_pChild;
 Node* m_pPrev;
 Node* m_pNext;
/* ========================= Code Ends ========================= */

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at