views:

237

answers:

2

I am trying to write a simple STL iterator for CArray MFC class using boost iterator adaptor. This is my code:

#include <boost/iterator/iterator_adaptor.hpp>
#include <afxtempl.h>

class CArrIter : public boost::iterator_adaptor< CArrIter , 
    int, 
    int,
    boost::random_access_traversal_tag >
{
public:
    CArrIter(CArray<int,int>& arr, int index = 0) : m_arr(arr)
    {
        this->base_reference() = index;
    }

private:
    friend class boost::iterator_core_access;
    int dereference() const{
        return m_arr.GetAt(base());
    }


private:
    CArray<int,int>& m_arr;
};

This compiles fine with VC9 compiler. But when I try compiling this with VC7 I get the following error:

\include\boost\iterator\iterator_traits.hpp(49) : erro r C2039: 'difference_type' : is not a member of 'boost::detail::iterator_traits< Iterator>' with [ Iterator=int ]

\include\boost\mpl\eval_if.hpp(41) : see refer ence to class template instantiation 'boost::iterator_difference' bein g compiled with [ Iterator=int ]

.... Some more ....

Any clues what could be wrong? I have to include some other header files? I am quite new to boost library.

A: 

It could be related to the random access behavior not having everything it needs to traverse the container. The 'iterator_adaptor requirements' section of this link might help:

Boost: Iterator Adapter

I'm not sure if int is assignable, so I wonder what would happen if you changed int to int&.

A couple more ideas:

  • Are you using the same version of the Boost library with both compilers?
  • Does making dereference() protected or public help?
richj
+4  A: 

I think that the second template parameter of boost::iterator_adaptor<> has to be a valid iterator type, try using int* instead of int.

Manuel
Indeed, the second argument (Base) will be used to determine the default types for the rest, passing it through the equivalent of `std::iterator_traits`. Since OP doesn't specify `difference_type`, compilation fails when boost tries to do it for him with `boost::detail::iterator_traits<int>`
UncleBens
but why would it compile with VC9 then?
Naveen
@Naveen: I don't know. But now that I think about it, maybe what you need is actually iterator_facade?http://www.boost.org/doc/libs/1_41_0/libs/iterator/doc/iterator_facade.html
Manuel
@Manuel: thanks, I'll give that also a try.
Naveen