I understand the user's question completely. He/She wants a custom container class that exposes to the user an STL-like interface. The standard STL containers do not suffice in some manner and so they are not a suitable choice.
For example, I have an interface class for a 'Dataline' called IDataline. An implementation of the IDataLine interface takes at construction a delimited string, parses it and exposes the list of fields. via a const_iterator with forward_iterator_tag semantics.
No STL container can do this out of the box.
Furthermore, my client class wants to be able to iterate through two data line fields and compare them, field by field.
I have defined the IDataline interface as follows:
1 class IDataline
2 {
3 public:
4 class const_iterator
5 {
6 public:
7 virtual const_iterator& operator=(const const_iterator& rhs) =0;
8 virtual bool operator==(const const_iterator& rhs) =0;
9 virtual bool operator!=(const const_iterator& rhs) =0;
10 virtual const_iterator& operator++() =0;
11 virtual const_iterator operator++(int) =0;
12 virtual const Field& operator*() =0;
13 virtual const Field* operator->() =0;
14 virtual const Field& operator[](size_t idx) =0;
15 virtual size_t offset() =0;
16 };
17
18 virtual const_iterator begin() =0;
19 virtual const_iterator end() = 0;
20 };
The problem can be seen on lines 11, 18 and 19 - we need to be able to return a const_iterator, which requires a copy constructor, but since it is virtual, there is neither a default nor a copy constructor (for the interface type) and the compiler (properly) balks.
You might argue that I could define begin() and end() as:
virtual const_iterator& begin() = 0;
virtual const_iterator& end() = 0;
Then, I can create two instances of the specialized iterator in the specialized host and return references (or pointers for those who prefer those), to get this to work for my case.
The problem is that such an implementation does not meet all the requirements for forward iterators and will break in more general use cases that rely on forward iterator semantics.
There are two ways I've found after a bit of thought (and colleague consult) that this can be addressed:
Does your host class have to be completely abstract? If you can minimize the abstraction to just the behavior you need to vary, then your instantiable host can embed a concrete iterator (because an instance of the class itself can be instantiated) and you have what you need.
Eduardo Leon above indicates that the iterator itself can be wrapped using the pimpl (Pointer to an implementation) idiom. While there is a lot of material available that thoroughly describes that technique, Basilev, in comments below Leon's suggestion, indicates that he doesn't believe the pimpl idiom will work. My colleague offered to me a technique to try, so I will work up a tested sample. If it works, I'll share it. If not, I'll detail the experience gained in the sample and wait for someone more knowledgeable to weigh in on whether or not pimpl is applicable in this case.
autokeyword can be used to imply the correct iterator type for a container by the rvalue but that may not be enough for your needs.