|
Boost : |
From: George A. Heintzelman (georgeh_at_[hidden])
Date: 2001-11-21 10:29:42
Henrik,
> I'm struggling with the boost::iterator_adaptors, in detail with the
> indirect_iterator stuff.
> During my search in the boost group, George A. Heintzelman (message
> #18183) depicted nearly the same I need. The difference is I'm using
> shared pointers as the vector template argument.
In that message I supplied a patch, which has been applied in the CVS
version and looks like it made it into 1.25.1. Your code compiles with
g++-3.0.2 with boost version 1.25.0 and my patch applied, with a small
change indicated below (not relevant to the error you were getting, I
think). If you applied the patch and it still doesn't compile, then
there is probably some other problem related to Borland's compiler's
idiosyncracies...
Good luck,
George Heintzelman
georgeh_at_[hidden]
> //--------------------------------------------------------------------
> #include <iostream>
> #include <boost/utility.hpp>
> #include <boost/smart_ptr.hpp>
> #include <vector>
> #include <boost/iterator_adaptors.hpp>
> #pragma hdrstop
>
> //--------------------------------------------------------------------
>
> class foo : boost::noncopyable
> {
> public:
> foo(const int a_) : m_a(a_) {}
>
> int get() { return m_a; }
>
> private:
> int m_a;
> };
>
>
>
>
> #pragma argsused
> int main(int argc, char* argv[])
> {
> typedef boost::shared_ptr<foo> foo_ptr_t;
> typedef std::vector<foo_ptr_t> foo_ptr_vect_t;
> typedef foo_ptr_vect_t::iterator vect_iterator;
>
> typedef boost::indirect_iterator_pair_generator<
> vect_iterator,
> foo, foo&, const foo&,
> std::random_access_iterator_tag,
> foo*,
> const foo*> pair_indirectors;
> typedef pair_indirectors::iterator indirector;
>
> foo_ptr_vect_t v(4);
>
> for(std::size_t i=0; i<v.size(); ++i)
> v[i].swap( foo_ptr_t(new foo(i)) );
This is not legal, since swap must take a non-const reference and you
are creating a temporary here. g++ 3.0.2 complained. I replaced this
with;
for(std::size_t i=0; i<v.size(); ++i) {
foo_ptr_t new_foo(new foo(i));
v[i].swap( new_foo );
}
and it compiled fine.
>
> // the following line produces an compiler error
> indirector i(v.begin()),
> // indirector end(v.end());
>
> // while(i!=end)
> // {
> // std::cout << i->get() << '\n';
> // ++i;
> // }
>
> return 0;
> }
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk