From be5aaaae7b9f045ad0df4a38b3631e456263042a Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Fri, 16 Feb 2001 05:33:21 +0000 Subject: [PATCH] More edits for clarity. Added const/non-const example. [SVN r9223] --- iterator_adaptors.htm | 104 ++++++++++++++++++++++++++++++------------ 1 file changed, 76 insertions(+), 28 deletions(-) diff --git a/iterator_adaptors.htm b/iterator_adaptors.htm index fe6cae2..e385520 100644 --- a/iterator_adaptors.htm +++ b/iterator_adaptors.htm @@ -155,29 +155,30 @@ struct iterator_adaptor; Value The value_type of the resulting iterator, unless const. If - Value is const X, a conforming compiler makes the - value_type non-const Xconst X the + value_type will be (non-const) X[1].
Default: - std::iterator_traits<BaseType>::value_type - - - Pointer - - The pointer type of the resulting iterator, and in - particular, the result type of operator->().
- Default: If Value was supplied, then Value*, - otherwise std::iterator_traits<BaseType>::pointer. + std::iterator_traits<BaseType>::value_type [2] Reference The reference type of the resulting iterator, and in - particular, the result type of operator*().
+ particular, the result type of operator*().
Default: If Value is supplied, Value& is used. Otherwise std::iterator_traits<BaseType>::reference is used. + + Pointer + + The pointer type of the resulting iterator, and in + particular, the result type of operator->().
+ Default: If Value was supplied, then Value*, + otherwise std::iterator_traits<BaseType>::pointer. + Category @@ -287,7 +288,7 @@ struct iterator_adaptor;
-struct default_iterator_policies
+struct default_iterator_policies
 {
   template <class Reference, class BaseType>
   Reference dereference(type<Reference>, const BaseType& x) const
@@ -512,9 +513,9 @@ int main(int, char*[])
     iterator_adaptor can make it easy. The rules are as follows: 
 
     
 
+    

Example

+ +

The Projection Iterator portion of this + library lets you create iterators which, when dereferenced, apply a function to the result of + dereferencing their base iterator. The projection_iterator_pair_generator template + is a special two-type generator for mutable and constant versions of a + projection iterator. It is defined as follows: +

+
+template <class Function, class Iterator, class ConstIterator>
+struct projection_iterator_pair_generator {
+    typedef typename AdaptableUnaryFunction::result_type value_type;
+    typedef projection_iterator_policies<Function> policies;
+public:
+    typedef iterator_adaptor<Iterator,policies,value_type> iterator;
+    typedef iterator_adaptor<ConstIterator,policies,value_type,
+        const value_type&,const value_type*> type;
+};
+
+
+ +

It is assumed that the Iterator and ConstIterator arguments are corresponding mutable +and constant iterators.

    +
  • +Clearly, then, the +projection_iterator_pair_generator's iterator and +const_iterator are interoperable, since +they share the same Policies and since Category and +Distance as supplied by std::iterator_traits through the +default template parameters to +iterator_adaptor should be the same. + +
  • Since Iterator can presumably be converted to +ConstIterator, the projection iterator will be convertible to +the projection const_iterator. + +
  • Since projection_iterator_policies implements only the +dereference operation, and inherits all other behaviors from default_iterator_policies, which has +fully-templatized equal, less, and distance +operations, the iterator and const_iterator can be freely +mixed in comparison and subtraction expressions. + +
+

Challenge

There is an unlimited number of ways the the iterator_adaptors @@ -620,24 +667,24 @@ bool operator==(const iterator_adaptor<B1,P,V1,R1,P1,C,D>&,

Notes

-

[1] If your compiler does not support partial - specialization and the base iterator is a builtin pointer type, then you - will not be able to use the default for Value and will need to - explicitly specify this type. - -

[2] The standard specifies that the value_type +

[1] The standard specifies that the value_type of const iterators to T (e.g. const T*) is non-const T, while the pointer and reference types for all Forward Iterators - are const T* and const T&, respectively. Stripping - the const-ness of Value is designed to allow you to - easily make a const iterator adaptor by supplying a const - type for Value, and allowing the defaults for the Pointer - and Reference parameters to take effect. Although compilers that - don't support partial specialization won't do this for you, having a + "http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterators are + const T* and const T&, respectively. Stripping the + const-ness of Value allows you to easily + make a const iterator adaptor by supplying a const type + for Value, and allowing the defaults for the Pointer and + Reference parameters to take effect. Although compilers that don't + support partial specialization won't strip const for you, having a const value_type is often harmless in practice. +

[2] If your compiler does not support partial + specialization and the base iterator is a builtin pointer type, you + will not be able to use the default for Value and will have to + specify this type explicitly. +

[3] The result type for the operator->() depends on the category and value type of the iterator and is somewhat complicated to describe. But be assured, it works in a stardard conforming @@ -681,6 +728,7 @@ bool operator==(const iterator_adaptor<B1,P,V1,R1,P1,C,D>&, -->