Erasable by C ++ output iterator

How to erase a type from output iterators like std::insert_iterator and std::back_insert_iterator ? Is it possible to use boost any_iterator for this?

 #include <boost/range.hpp> #include <boost/range/detail/any_iterator.hpp> #include <vector> typedef boost::range_detail::any_iterator< int, boost::incrementable_traversal_tag, int &, std::ptrdiff_t > It; int main() { std::vector<int> v; It outIt( v.begin() ); // compiles It inserter( std::back_inserter(v) ); // does not compile return 0; } 
+6
source share
2 answers

any_iterator not intended for use with output iterators, which means back_insert_iterator (or, in any case, input iterators).

back_insert_iterator defined as inherited from iterator<output_iterator_tag, void, void, void, void> , i.e. its value_type , reference_type , distance_type and pointer_type all void , but any_iterator expects that it can indirectly through its iterator support to a any_iterator value. Perhaps this would be better named any_value_iterator ; but then this is the template of the detail class.

+6
source

So, I implemented my own using Boost.

 #include <boost/function_output_iterator.hpp> #include <boost/function.hpp> template < class T > class AnyInserter : public boost::function_output_iterator< boost::function< void ( const T & value ) > > { private: typedef typename boost::function_output_iterator< boost::function< void ( const T & value ) > > BaseType; template < class OutIt > struct Insert { Insert( OutIt it ) : m_it(it) {} void operator () ( const T & value ) { m_it++ = value; } OutIt m_it; }; public: template < class OutIt > explicit AnyInserter( const OutIt & it ) : BaseType( Insert< OutIt >(it) ) {} }; template < class OutIt > inline AnyInserter< typename OutIt::container_type::value_type > makeAnyInserter( const OutIt & it ) { return AnyInserter< typename OutIt::container_type::value_type >(it); } 
+3
source

Source: https://habr.com/ru/post/916882/


All Articles