On 9/28/05, David Abrahams wrote:
:) Yes, that's expected. When dereferenced, indirect_iterator gives
you a reference onto an existing value.
Why isn't this working?
It wasn't designed to do that.
But it is possible to do input with an indirect_iterator no? I mean, there's
the example from the
documentation (somewhat summarized):
char* pointers_to_chars[N];
char* pointers_to_mutable_chars[N];
boost::indirect_iterator
mutable_indirect_first(pointers_to_mutable_chars),
mutable_indirect_last(pointers_to_mutable_chars + N);
boost::indirect_iterator
const_indirect_first(pointers_to_chars),
const_indirect_last(pointers_to_chars + N);
std::transform(const_indirect_first, const_indirect_last,
mutable_indirect_first, std::bind1st(std::plus<char>(), 1));
There you do use mutable_indirect_first to copy data into an array of
pointers. Ok, my approach using the
helper function was most likely too naive, though I'm not entirely sure why
that one doesn't work.
I tried doing the same with a vector of smartpointers, but I can't get that
to work. I don't really get how to do the
"char * const *" equivalent, substituting "const iterator"
doesn't compile. Making it non-const compiles,
but crashes. This is the code:
typedef boost::shared_ptr< double > d_ptr;
typedef std::vector< d_ptr >::iterator const d_it;
std::vector< d_ptr > v( 10 );
std::vector< d_ptr > w( 10 );
boost::indirect_iterator< d_it > mutable_indirect_first( w.begin() ),
mutable_indirect_last( w.end() );
boost::indirect_iterator< d_it, double const> const_indirect_first( v.begin()
),
const_indirect_last( v.end() );
std::copy( c_indirect_first, c_indirect_last, mut_indirect_first );
And this is the error message:
/usr/include/boost/shared_ptr.hpp:247: typename
boost::detail::shared_ptr_traits<T>::reference
boost::shared_ptr<T>::operator*() const
[with T = double]: Assertion `px != 0' failed.
I must say it's all a bit beyond me right now. Could you explain why things
go wrong here?
Also, some way to get it to work as a back_insert_iterator would be
interesting, does this exist (aside from writing your own)?
Neither exists, but for either one, writing your own is possible using
iterator_facade. The tutorial is at
http://www.boost.org/libs/iterator/doc/iterator_facade.html#tutorial-example
Ok, I'll look into it, thanks.
--
Alex Borghgraef