Boost logo

Boost :

From: David B. Held (dheld_at_[hidden])
Date: 2002-09-24 18:30:25


"Bohdan" <warever_at_[hidden]> wrote in message
news:amqo40$888$1_at_main.gmane.org...
> [...]
> IMHO i like this idea. The simplest solutions are always the best , but
> are you sure that this two idioms (iterators & pointers ) should be mixed
?
> Does standart allow such iterators ?

But, of course. Pointers into arrays can be passed as iterators to standard
algorithms. That was part of the motivation for the iterator design...to
mimic builtin pointer types. My STL implementation (RW) already has a
specialization for T* (I assume it's standard).

> PS: not too serious:
> If "anyone" implements iterator_traits for a smart_ptr, than
> what will be the name for its iterator category:
> "uninterative_iterator_category" ?

I suppose it depends on what the smart pointer can do. For instance, the
T* specialization declares T* to be a random_access_iterator. For a
smart pointer to an array (like shared_array), I suppose it should also be
a random_access_iterator. Not sure for scalar smart pointers. I'm not
sure if user-defined partial specializations of std::iterator_traits is
legal or
not, but I guess it would look like this:

struct trivial_iterator_tag { };

template <class T>
struct iterator_traits< boost::shared_ptr<T> >
{
   typedef T value_type;
   typedef std::ptrdiff_t difference_type;
   typedef T* pointer;
   typedef T& reference;
   typedef trivial_iterator_tag iterator_category;
};

Then, of course, you should be able to say:

template < typename Iterator > void f( Iterator i )
{
      std::iterator_traits<Iterator>::reference ref = *i;
      // ... do something with ref ...
}

Dave


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