I have a class like this,
template <typename Node>
class BSTIteratorBase : public boost::iterator_facade<
BSTIteratorBase<Node>,
typename Node::value_type,
boost::forward_traversal_tag
>
{ ...
value_type& dereference() const
{ return const_cast<value_type&>( nodePtr_->value_ ); } // Ouch! const_iterator may modify
... };
value_type
does not depend on the constness of the BSTNode
class. That is why I had to keep the const_cast<value_type&>()
part. How can I make sure that const_iterator
return a const_ref
but iterator
returns a modifiable ref
? Here are the relevant typedefs,
template <typename T>
class BinarySearchTree
{
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef BSTNode<T> node_type;
typedef BSTNode<T>& node_reference;
typedef BSTNode<T>* node_pointer;
typedef BSTIteratorBase<BSTNode<T>> iterator;
typedef BSTIteratorBase<const BSTNode<T>> const_iterator;
And the node class,
template <typename T>
class BSTNode
{
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef BSTNode node_type;
typedef BSTNode* node_pointer;
// ctors, dtor
private:
template <class> friend class BSTIteratorBase;
template <class> friend class BinarySearchTree;
T value_;
node_pointer leftPtr_;
node_pointer rightPtr_;
};
You can use a metafunction that constifies
value_type
if its enclosing type is const: