The Traversable Mix-in Templates

As discussed in the previous section, any collection that implements one of the traversable interfaces is accessible through the polymorphic iterators. To ease the task of mixing in and implementing these interfaces, SFL provides two helpers:

  • const_traversable<>

  • traversable<>

The purpose of these templates is to make any STL-compliant collection usable with the polymorphic iterators by implementing IConstTraversableT<> and ITraversableT<>, respectively. These templates are an example of the decorator design pattern, discussed in Wrappers (Decorators) Their approach is to use multiple derivation from the collection type argument and the appropriate traversable interface, implementing its members. Because derivation is used, the original collection interface is preserved. So, collection declarations can be wrapped with one of the templates without impacting existing code. Adapting STL collections to polymorphic iteration illustrates how these templates are used.

Adapting STL collections to polymorphic iteration

traversable < vector<int> > v;

or

const_traversable < deque<int> > dq;

You can manually derive and implement the traversable interfaces for your own collection classes. Or, make your own collections STL-compliant so the traversable mix-in templates will work with them. To be STL-compliant in this sense, your collection class must have the nested iterators const_iterator, iterator, reverse_iterator, and const_reverse_iterator with STL’s calling conventions.