Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2000-06-12 23:09:05


template <class T, class V, class D = boost::detail::empty_base>
struct dereferenceable : D
{
  V* operator->() const
  {
    return &*static_cast<const T&>(*this);
  }
};

Notice that the return type is V*: plain, unadorned V*.
The standard quoth:

[iterator traits is specialized] for pointers to const as:

template<class T> struct iterator_traits<const T*> {
  typedef ptrdiff_t difference_type;
  typedef T value_type;
  typedef const T* pointer;
  typedef const T& reference;
  typedef random_access_iterator_tag iterator_category;
};

Notice that the value_type is plain, unadorned T. I guess that means that
the standard library expects value_type, even for const iterators, to be
non-const. Exhaustively searching the standard for reliance on this "fact"
turns up nothing of any consequence (look at uninitialized_copy and decide
for yourself). This means that when we create the following class:

struct X : boost::random_access_iterator_helper<X, Y, std::ptrdiff_t, const
Y* const Y&>
{
  ...
};

We get an iterator through which we can access Y as non-const, but whose
iterator_traits look just like the iterator_traits for const Y*. Problem?
I guess there shouldn't be one, (since we can ask the user to pass const Y
as the value_type) but I am not confident...

I suppose we could change the interface to dereferenceable to take the
pointer type instead of the value_type.

-Dave


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk