Re: Iterator adaptor interoperability
Dave Abrahams wrote:
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; };
Thank you for the quick and clear reply! I'm now having a very similar problem with a slightly more complicated example (that builds on the first): template< class Iterator > struct harder_iterator { typedef simple_iterator< Iterator >::type inner_type; typedef boost::indirect_iterator< inner_type > type; static type Create( const Iterator& iter ) { return type( inner_type( iter ) ); } }; This "composite" iterator not only has problems with comparison, but assignment from non-const to const as well. Is there a way to handle this case? Thanks, 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:
Dave Abrahams wrote:
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; };
Thank you for the quick and clear reply! I'm now having a very similar problem with a slightly more complicated example (that builds on the first):
template< class Iterator > struct harder_iterator { typedef simple_iterator< Iterator >::type inner_type; typedef boost::indirect_iterator< inner_type > type;
static type Create( const Iterator& iter ) { return type( inner_type( iter ) ); } };
This "composite" iterator not only has problems with comparison, but assignment from non-const to const as well. Is there a way to handle this case?
Need more details. Post a reproducible test that fails compilation and I might be able to solve your problem. -- Dave Abrahams Boost Consulting www.boost-consulting.com
--- David Abrahams <dave@boost-consulting.com> wrote:
Need more details. Post a reproducible test that fails compilation and I might be able to solve your problem.
Okay, here's all the code for the test: #include <map> #include <boost/iterator/transform_iterator.hpp> #include <boost/iterator/indirect_iterator.hpp> template< class Pair > struct select_second { typedef typename Pair::second_type result_type; typename Pair::second_type& operator( )( Pair& pair ) { return pair.second; } const typename Pair::second_type& operator( )( const Pair& pair ) const { return pair.second; } }; template< class Iterator > struct map_iterator { typedef boost::transform_iterator< select_second< typename Iterator::value_type >, Iterator > type; static type Create( const Iterator& iter ) { return type( iter ); } }; template< class Iterator > struct pointer_map_iterator { typedef typename map_iterator< Iterator >::type inner_type; typedef boost::indirect_iterator< inner_type > type; static type Create( const Iterator& iter ) { return type( inner_type( iter ) ); } }; typedef std::map< int, int* > MapType; typedef pointer_map_iterator< MapType::iterator > iterator_generator; typedef pointer_map_iterator< MapType::const_iterator
const_iterator_generator; typedef iterator_generator::type iterator; typedef const_iterator_generator::type const_iterator;
struct Foo { iterator begin( void ) { return iterator_generator::Create( m.begin( ) ); } iterator end( void ) { return iterator_generator::Create( m.end( ) ); } MapType m; }; void foo( void ) { Foo f; for( const_iterator i = f.begin( ); i != f.end( ); ++i ) { } } When compiled with VC7.1, this produces: src\util\test.cpp(53) : error C2440: 'initializing' : cannot convert from 'iterator' to boost::indirect_iterator<Iterator>' with [ Iterator=pointer_map_iterator<std::map<int,int *>::const_iterator>::inner_type ] No constructor could take the source type, or constructor overload resolution was ambiguous src\util\test.cpp(53) : error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'const_iterator' (or there is no acceptable conversion) Thanks, 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:
--- David Abrahams <dave@boost-consulting.com> wrote:
Need more details. Post a reproducible test that fails compilation and I might be able to solve your problem.
Okay, here's all the code for the test:
Thomas sez: From: Thomas Witt <witt@acm.org> Subject: Re: Iterator adaptor interoperability To: boost-users@lists.boost.org Date: Fri, 27 Feb 2004 16:35:52 -0800 Hi Kris, Kris Braun wrote:
--- David Abrahams <dave@boost-consulting.com> wrote:
Need more details. Post a reproducible test that fails compilation and I might be able to solve your problem. Okay, here's all the code for the test:
Either I am going nuts or this is a compiler problem adding the following line before foo makes the code compile for me BOOST_STATIC_ASSERT((boost::is_convertible<MapType::iterator, MapType::const_iterator>::value));
void foo( void ) { Foo f; for( const_iterator i = f.begin( ); i != f.end( ); ++i ) { } }
Could you please check whether you get the same result. Thomas -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (2)
-
David Abrahams
-
Kris Braun