Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2004-02-24 04:52:56


Kris Braun <kris_braun_at_[hidden]> writes:

> Hello,
>
> I've spent the day trying to get my iterators working
> after upgrading to 1.31.0. I'm almost there, but I
> can't seem to get const and non-const versions to
> compare with each other. I've tried about everything I
> can think of, so I'll just post a minimal test that
> fails in hopes that someone can see my mistake:
>
> #include <iostream>
> #include <list>
> #include <boost/iterator/transform_iterator.hpp>
>
> template< class Type >
> struct do_nothing {
> typedef Type result_type;
>
> result_type operator( )( const Type& t )
> { return t; }
> };
>
> template< class Iterator >
> class simple_iterator :
> public boost::transform_iterator< do_nothing<
> typename Iterator::value_type >, Iterator >
> {

You cannot expect to derive an iterator from transform_iterator. The
library's generated operator!= is templated, and will only work for
two transform_iterators, not derived classes (not to mention other
problems with that approach).

Your best bet is to write an type generator:

template <class Iterator>
struct simple_iterator
{
    typedef boost::transform_iterator<
        do_nothing< typename Iterator::value_type >
      , Iterator
> type;
};

simple_iterator<whatever>::type is now the iterator you wanted.

HTH,

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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