0

Basically, I'm trying to write a base class that has a generic iterator in.

So apparently template virtual functions don't work, that's why I tried boost::any, but it is still not overloading Iterator's get() method (it keeps throwing the Exception defined below). I also can't seem to make it pure virtual because the compiler complains about 'virtual iterator begin() = 0;' (it says 'cannot create instance of abstract class Iterator').

I have very specific constraints that I have to / want to adhere to, so alternative solutions to the problem might be looked at, but probably can't be used.

Here is a part of the code:

  template <class T, class C>
  class Iterator
  {
    public:
      virtual T& operator*() { T *t = boost::any_cast<T>(&get()); return *t; };
      virtual boost::any& get() { throw new Exception("get not overridden!!"); };
      //virtual boost::any& get() = 0;
    protected:
      C::iterator iter;
  };

  template <class T>
  class VectorIterator: public Iterator<T, std::vector<T> >
  {
    public:
      VectorIterator(std::vector<T>::iterator iterator): Iterator<T, std::vector<T> >(iterator) { };
      virtual boost::any& get() { return *iter; };
  };

  template <class Value, class Container>
  class Repository
  {
    public:
      typedef Iterator<Value, Container> iterator;

      virtual iterator begin() = 0;
      virtual iterator end() = 0;
  };


  class SomeRepository : public Repository<RandomClass, std::vector<RandomClass> >
  {
    public:
      iterator begin() { return VectorIterator<RandomClass>(items.begin()); };

      iterator end() { return VectorIterator<RandomClass>(items.end()); };
  }

So, why isn't it working and (how) can I fix it?

PS: I know this is very similar to another question, but I couldn't exactly fit this into a comment and didn't really know where else I could put it.

UPDATE:

So I managed to figure something out by letting get() return a void (eww) pointer and forgetting about boost::any. But it still calls Iterator::get instead of VectorIterator::get (as was my original problem) (when I try to use something like *someRepository.begin()), does anyone know why?

5
  • I'm curious. What is this supposed to achieve? If you want to type erase on iterators there are much better methods available.
    – pmr
    Oct 3, 2012 at 16:26
  • 7
    throw new Exception(...); -- please don't...
    – Xeo
    Oct 3, 2012 at 16:26
  • @pmr There is existing functionality in our code which iterates through all elements of the Repository, I thought a better way would be to have a generic iterator for this. Oct 4, 2012 at 6:51
  • @Dukeling If you really need a type-erased iterator (which you probably don't), you should have a look at thbecker.net/free_software_utilities/… Chances are you don't need it though.
    – pmr
    Oct 4, 2012 at 8:50
  • 1
    The reason you have the error is that you return Iterator, which is abstract. C++ is a language much different from e.g. C# or Java (they may be superficially syntactically similar but that's it). Returning Iterator will only ever return a value of dynamic type Iterator.
    – Luc Danton
    Oct 4, 2012 at 9:27

2 Answers 2

3

You cannot initialize a boost::any& from *iter, much like you cannot initialize float& from a value of type int. In both case there is no actual object of the desired type to refer to.

You can change get to return boost::any (in which case the conversion from *iter will happen), but then your operator* has an issue in that the T it wants to refer to (again, reference type) needs to outlive the function call. One solution is to store the result of calling get somewhere, or to follow through with the same change to get by making operator* also return a value (T) rather than a reference (T&). Those would be value semantics, and not a 'shallow' view into the underlying container. Assigning to the result of operator*, assuming it's correct, would not affect the element of the container.

Another possibility is to make get return a boost::any that is guaranteed to store a shallow view into the element, e.g.

virtual boost::any get() { return &*iter; }

(return boost::ref(*iter); is another possibility)

Care must be then taken inside operator* to correctly restore the expected value, e.g. return *boost::any_cast<T*>(get()); (resp. return boot::any_cast<boost::reference_wrapper<T> >(get()).get();). This has reference semantics, where returning T& would be fine. Operations on the result of operator* could potentially affect elements of the container.

0

If you return Iterator in 'iterator begin()' (so returning by value), it will convert the return to Iterator, thus it will no longer be of type VectorIterator and obviously it will not call VectorIterator's get method (at least that's what I think). Returning by reference or pointer should work just fine.

Now I just need to figure out a workaround. Returning by reference won't work since I'm returning a local variable in VectorIterator::begin(). I suppose I'll have to return by pointer unless someone else has a better suggestion.

1
  • Return by boost::shared_ptr might be a better idea. Oct 4, 2012 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.