Boost iterator_facade and forward declarations

162 views Asked by At

I want to create a custom container that supports iterators. It looks like this:

class SomeContainer {
    ...
public:
    typedef SomeIterator iterator;
    iterator begin() { ... }
    iterator end() { ... }
};

Then I create an iterator for this:

class SomeIterator: public boost::iterator_facade<
         SomeIterator, 
         SomeType,
         boost::bidirectional_traversal_tag> {
    ...
}

The problem with this is the following. If I declare SomeContainer before SomeiIterator and forward declare SomeIterator, then the compiler complains st the begin() and end() methods that SomeIterator is an incomplete type. However, if I do it the other way, then the problem is the other way around: SomeContainer is incomplete.

  1. Is it possible to solve this problem while having both classes fully header-only and all methods implicit inline (they mostly contain only a few lines)?
  2. If not, is it possible to solve it with factoring out some methods into cpp files?
1

There are 1 answers

1
Luc Danton On BEST ANSWER

It is possible to satisfy your first requirement partially, in that you can define everything in the header except for begin and end that will need to be declared inline and defined outside the SomeContainer definition, and after the SomeIterator definition (which completes the type). This assumes you keep the current order of definition (SomeContainer before SomeIterator), which I suggest you keep.

Otherwise, you can certainly ditch inline (implicit or otherwise) and define outside the class definitions. By doing so, both types will be complete from those definitions.