template <class T> class input_iterator_archetype { private: typedef input_iterator_archetype self; public: typedef std::input_iterator_tag iterator_category; typedef T value_type; struct reference { operator const value_type&() const { return static_object<T>::get(); } }; typedef const T* pointer; typedef std::ptrdiff_t difference_type; self& operator=(const self&) { return *this; } bool operator==(const self&) const { return true; } bool operator!=(const self&) const { return true; } reference operator*() const { return reference(); } self& operator++() { return *this; } self operator++(int) { return *this; } };Generic algorithms are often tested by being instantiated with a number of common input types. For example, one might apply std::stable_sort() with basic pointer types as the iterators. Though appropriate for testing the run-time behavior of the algorithm, this is not helpful for ensuring concept coverage because C++ types never match particular concepts, they often provide much more than the minimal functionality required by any one concept. That is, even though the function template compiles with a given type, the concept requirements may still fall short of covering the functions actual requirements. This is why it is important to compile with archetype classes in addition to testing with common input types.
The following is an excerpt from stl_concept_covering.cpp that shows how archetypes can be used to check the requirement documentation for std::stable_sort(). In this case, it looks like the CopyConstructible and Assignable requirements were forgotten in the SGI STL documentation (try removing those archetypes). The Boost archetype classes have been designed so that they can be layered. In this example the value type of the iterator is composed out of three archetypes. In the archetype class reference below, template parameters named Base indicate where the layered archetype can be used.
{ typedef less_than_comparable_archetype< sgi_assignable_archetype<> > ValueType; random_access_iterator_archetype<ValueType> ri; std::stable_sort(ri, ri); }Next: Programming with Concepts
Copyright © 2000 | Jeremy Siek(jsiek@osl.iu.edu) Andrew Lumsdaine(lums@osl.iu.edu) |