Iterator adaptor interoperability
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 > { private: typedef boost::transform_iterator< do_nothing< typename Iterator::value_type >, Iterator > super_t; public: simple_iterator( void ) { } simple_iterator( Iterator iter ) : super_t( iter ) { } template< class Iterator2 > simple_iterator( const simple_iterator< Iterator2 >& iter ) : super_t( iter.base( ) ) { } }; typedef std::list< int > ListType; typedef simple_iterator< ListType::iterator > iterator; typedef simple_iterator< ListType::const_iterator > const_iterator; struct Foo { iterator begin( void ) { return iterator( m.begin( ) ); } const_iterator end( void ) const { return const_iterator( m.end( ) ); } ListType m; }; void foo( void ) { Foo f; for( iterator i = f.begin( ); i != f.end( ); ++i ) { } } which on VC7.1 yield src\util\test.cpp(53) : error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'iterator' (or there is no acceptable conversion) Any help would be much appreciated! Cheers, Kris __________________________________ Do you Yahoo!? Yahoo! Mail SpamGuard - Read only the mail you want. http://antispam.yahoo.com/tools
Kris Braun <kris_braun@yahoo.com> 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
participants (2)
-
David Abrahams
-
Kris Braun