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.
- 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)?
- If not, is it possible to solve it with factoring out some methods into cpp files?
It is possible to satisfy your first requirement partially, in that you can define everything in the header except for
begin
andend
that will need to be declaredinline
and defined outside theSomeContainer
definition, and after theSomeIterator
definition (which completes the type). This assumes you keep the current order of definition (SomeContainer
beforeSomeIterator
), 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.