I have a linked list structure:
struct SomeLinkedList
{
const char* bar;
int lots_of_interesting_stuff_in_here;
DWORD foo;
SomeLinkedList* pNext;
};
This is part of the existing API, and I cannot change it.
I would like to add iterator support. The library boost::iterator_facade<>seemed perfect for this purpose.
class SomeIterator
: public boost::iterator_facade< SomeIterator,
const SomeLinkedList,
boost::forward_traversal_tag >
{
public:
SomeIterator() : node_( NULL ) {};
explicit SomeIterator( const SomeLinkedList* p ) : node_( p ) {};
private:
friend class boost::iterator_core_access;
void increment() { node_ = node_->pNext; };
bool equal( SomeIterator const& other ) const { /*some comparison*/; };
SomeLinkedList const& dereference() const { return *node_; };
SomeLinkedList const* node_;
}; // class SomeIterator
The goal is to be able to use it in standard library functions such as std::for_each
void DoSomething( const SomeLinkedList* node );
SomeLinkedList* my_list = CreateLinkedList();
std::for_each( SomeIterator( my_list ), SomeIterator(), DoSomething );
Unfortunately, I am getting an error trying to pass a list by value, not a pointer.
error C2664: 'void (const SomeLinkedList *)' : cannot convert parameter 1 from 'const SomeLinkedList' to 'const SomeLinkedList *'
How can I change SomeIteratorto do it right?
Thanks PaulH
Edit: I tried this:
class SomeIterator
: public boost::iterator_facade< SomeIterator,
SomeLinkedList,
boost::forward_traversal_tag,
SomeLinkedList* >
{
// ...
but I get this error:
error C2664: 'boost::implicit_cast' : cannot convert parameter 1 from 'SomeLinkedList **' to 'boost::detail::operator_arrow_proxy<T>
Edit 2:
I tried changing the dereference type:
class SomeIterator
: public boost::iterator_facade< SomeIterator,
const SomeLinkedList,
boost::forward_traversal_tag >
{
// ...
const SomeLinkedList* dereference() const { return node_; };
but, I get the original error:
error C2664: 'void (const SomeLinkedList *)' : cannot convert parameter 1 from 'const SomeLinkedList' to 'const SomeLinkedList *'