diff --git a/reverse_iterator.htm b/reverse_iterator.htm index 6bb712e..f3c32d1 100644 --- a/reverse_iterator.htm +++ b/reverse_iterator.htm @@ -23,7 +23,7 @@ invoking operator--() moves the base iterator forward. The Boost reverse iterator adaptor is better to use than the std::reverse_iterator class in situations where pairs of - mutable/const iterators are needed (e.g., in containers) because + mutable/constant iterators are needed (e.g., in containers) because comparisons and conversions between the mutable and const versions are implemented correctly. @@ -194,6 +194,8 @@ letters in descending order: wroolllhed! reverse_iterator_generator::type(const BidirectionalIterator& it) + +

@@ -243,6 +245,72 @@ letters in ascending order: !dehllloorw
+

Constant/Mutable Iterator Interactions

+ +

One failing of the standard reverse_iterator + adaptor is that it doesn't properly support interactions between adapted + const and non-const iterators. For example: +

+
+#include <vector>
+
+template <class T> void convert(T x) {}
+
+// Test interactions of a matched pair of random access iterators
+template <class Iterator, class ConstIterator>
+void test_interactions(Iterator i, ConstIterator ci)
+{
+  bool eq = i == ci;               // comparisons
+  bool ne = i != ci;            
+  bool lt = i < ci;
+  bool le = i <= ci;
+  bool gt = i > ci;
+  bool ge = i >= ci;
+  std::size_t distance = i - ci;   // difference
+  ci = i;                          // assignment
+  ConstIterator ci2(i);            // construction
+  convert<ConstIterator>(i);       // implicit conversion
+}
+
+void f()
+{
+  typedef std::vector<int> vec;
+  vec v;
+  const vec& cv;
+
+  test_interactions(v.begin(), cv.begin());   // OK
+  test_interactions(v.rbegin(), cv.rbegin()); // ERRORS ON EVERY TEST!!
+
+
+Reverse iterators created with boost::reverse_iterator_generator don't have this problem, though: +
+
+  typedef boost::reverse_iterator_generator<vec::iterator>::type ri;
+  typedef boost::reverse_iterator_generator<vec::const_iterator>::type cri;
+  test_interactions(ri(v.begin()), cri(cv.begin()));   // OK!!
+
+
+Or, more simply, +
+
+  test_interactions(
+    boost::make_reverse_iterator(v.begin()), 
+    boost::make_reverse_iterator(cv.begin()));   // OK!!
+}
+
+
+ +

If you are wondering why there is no +reverse_iterator_pair_generator in the manner of projection_iterator_pair_generator, +the answer is simple: we tried it, but found that in practice it took +more typing to use reverse_iterator_pair_generator than to +simply use reverse_iterator_generator twice!

+ +


+ +

Revised 15 Feb 2001