I have a linked list structure:
struct SomeLinkedList
{
const char* bar;
int lots_of_interesting_stuff_in_here;
DWORD foo;
SomeLinkedList* pNext;
};
It is part of an existing API and I cannot change it.
I would like to add iterator support. The boost::iterator_facade<>
library seemed ideal for the 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 like std::for_each
void DoSomething( const SomeLinkedList* node );
SomeLinkedList* my_list = CreateLinkedList();
std::for_each( SomeIterator( my_list ), SomeIterator(), DoSomething );
Unfortunately, I'm getting an error saying it's trying to pass the list by value rather than by pointer.
error C2664: 'void (const SomeLinkedList *)' : cannot convert parameter 1 from 'const SomeLinkedList' to 'const SomeLinkedList *'
How can I change SomeIterator
to do to get this working correctly?
Thanks, PaulH
Edit: I've tried this:
class SomeIterator
: public boost::iterator_facade< SomeIterator,
SomeLinkedList,
boost::forward_traversal_tag,
SomeLinkedList* >
{
// ...
but I get this complier error:
error C2664: 'boost::implicit_cast' : cannot convert parameter 1 from 'SomeLinkedList **' to 'boost::detail::operator_arrow_proxy<T>
Edit 2:
I've tried modifying 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 *'