Huge chunks of the C++ Standard Library are built around generic programming techniques, most notably the containers, iterators, and algorithms of the STL. The approaches I describe in the talk are, quite plainly, object-oriented techniques. The proliferation of types in the code above arise from the tension between the generic and object-oriented programming.
Type erasure gives us a way to relieve that tension. In our filter_iterator, we don’t actually care about the actual type of iterator being wrapped. It isn’t exposed through the public interface, and so our client code doesn’t care about it either. In the public interface, we only care that it returns an int (or whatever) when dereferenced. Internally, the implementation only requires that wrapped iterator can be advanced by calling operator++ and compared for equality. If only there was a common base class along the lines of
class iterator_base</br>
{
virtual int& get(); // aka operator*()
virtual void advance(); // aka operator++()
virtual bool equal(const iterator_base& rhs); // aka operator==()
}
If you squint a bit, looks almost like the java.util.Iterator, doesn’t it? Perhaps Josh Bloch was on to something after all. Anyway … We can effectively introduce such a base class through the use of an adaptor or wrapper. If we use a template class with a non-template base class, we create a type specific wrapper to swaddle around the iterator, which we can then manipulate through the base class. Because the base class isn’t parameterised, it doesn’t expose anything about the wrapped iterator. By the classical computer science technique of an extra layer of indirection, we’ve slipped in a common base class down the side of our existing iterators.
template<typename value_type>
class iterator_holder</br>
{
public:
template<typename iterator_type>
iterator_holder(const iterator_type& iter) :
iter_(new holder<iterator_type>(iter)) { }
~iterator_holder() { delete iter_; }
...
value_type& get() const { return iter_->get(); }
void advance() { return iter_->advance(); }
private:
class holder_base
{
public:
virtual ~holder_base() { }
virtual holder_base* clone() const = 0;
bool compare(holder_base* rhs)
{
return (type() == rhs->type()) && (up_compare(rhs));
} // compare
virtual const std::type_info& type() const = 0;
virtual void advance() = 0;
virtual value_type& get() const = 0;
protected:
virtual bool up_compare(holder_base* rhs) const = 0;
}; // class holder_base
template<typename iterator_type>
class holder : public holder_base
{
public:
holder(const iterator_type& iter) : iter_(iter) { }
virtual holder_base* clone() const { return new holder(iter_); }
virtual const std::type_info& type() const
{
return typeid(iter_);
} // type
virtual void advance() { ++iter_; }
virtual value_type& get() const { return *iter_; }
protected:
virtual bool up_compare(holder_base* rhs) const
{
holder* r = dynamic_cast<holder*>(rhs);
return iter_ == r->iter_;
} // up_compare
private:
iterator_type iter_;
}; // class holder
holder_base* iter_;
}; // class iterator_holder
That’s enough code, and I won’t dwell on the details. The main thing to note that outermost class iterator_holder is parameterised on the value type, on what the held iterator points to, rather than the type of the iterator itself. The holder and holder_base are the template class with non-template base described above.
We can rewrite filter_iterator to use iterator_holder, and our example becomes
filter_iterator<int, Even> fb(vec.begin(), vec.end(), Even());
filter_iterator<int, Even> fe(vec.end(), vec.end(), Even());
for( ; fb != fe; ++fb)
{
std::cout << *fb << std::endl;
... do something else with *fb ...
} // for ...
But look at this. We can also write this
std::deque<int> deq;
...
filter_iterator<int, Even> fb(deq.begin(), deq.end(), Even());
filter_iterator<int, Even> fe(deq.end(), deq.end(), Even());
for( ; fb != fe; ++fb)
{
std::cout << *fb << std::endl;
... do something else with *fb ...
} // for ...
Exchanging the vector for a deque doesn’t require any other change. That’s rather a useful result.