Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/iterator/doc/issues.html @ 28

Last change on this file since 28 was 12, checked in by landauf, 17 years ago

added boost

  • Property svn:executable set to *
File size: 10.4 KB
Line 
1<?xml version="1.0" encoding="utf-8" ?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6<meta name="generator" content="Docutils 0.3.6: http://docutils.sourceforge.net/" />
7<title>Problem with is_writable and is_swappable in N1550</title>
8<link rel="stylesheet" href="default.css" type="text/css" />
9</head>
10<body>
11<h1 class="title">Problem with <tt class="literal"><span class="pre">is_writable</span></tt> and <tt class="literal"><span class="pre">is_swappable</span></tt> in <a class="reference" href="http://www.boost-consulting.com/writing/n1550.html">N1550</a></h1>
12<div class="document" id="problem-with-is-writable-and-is-swappable-in-n1550">
13<table class="field-list" frame="void" rules="none">
14<col class="field-name" />
15<col class="field-body" />
16<tbody valign="top">
17<tr class="field"><th class="field-name">Author:</th><td class="field-body">David Abrahams and Jeremy Siek</td>
18</tr>
19<tr class="field"><th class="field-name">Contact:</th><td class="field-body"><a class="reference" href="mailto:dave&#64;boost-consulting.com">dave&#64;boost-consulting.com</a>, <a class="reference" href="mailto:jsiek&#64;osl.iu.edu">jsiek&#64;osl.iu.edu</a></td>
20</tr>
21<tr class="field"><th class="field-name">Organization:</th><td class="field-body"><a class="reference" href="http://www.boost-consulting.com">Boost Consulting</a>, Indiana University Bloomington</td>
22</tr>
23<tr class="field"><th class="field-name">date:</th><td class="field-body">$Date: 2004/11/02 14:31:17 $</td>
24</tr>
25<tr class="field"><th class="field-name">Copyright:</th><td class="field-body">Copyright David Abrahams, Jeremy Siek 2003. Use, modification and
26distribution is subject to the Boost Software License,
27Version 1.0. (See accompanying file LICENSE_1_0.txt or copy
28at <a class="reference" href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</td>
29</tr>
30</tbody>
31</table>
32<div class="contents topic" id="table-of-contents">
33<p class="topic-title first"><a name="table-of-contents">Table of Contents</a></p>
34<ul class="simple">
35<li><a class="reference" href="#introduction" id="id1" name="id1">Introduction</a></li>
36<li><a class="reference" href="#proposed-resolution" id="id2" name="id2">Proposed Resolution</a></li>
37<li><a class="reference" href="#rationale" id="id3" name="id3">Rationale</a></li>
38</ul>
39</div>
40<div class="section" id="introduction">
41<h1><a class="toc-backref" href="#id1" name="introduction">Introduction</a></h1>
42<p>The <tt class="literal"><span class="pre">is_writable</span></tt> and <tt class="literal"><span class="pre">is_swappable</span></tt> traits classes in <a class="reference" href="http://www.boost-consulting.com/writing/n1550.html">N1550</a>
43provide a mechanism for determining at compile time if an iterator
44type is a model of the new Writable Iterator and Swappable Iterator
45concepts, analogous to <tt class="literal"><span class="pre">iterator_traits&lt;X&gt;::iterator_category</span></tt>
46for the old iterator concepts. For backward compatibility,
47<tt class="literal"><span class="pre">is_writable</span></tt> and <tt class="literal"><span class="pre">is_swappable</span></tt> not only work with new
48iterators, but they also are intended to work for old
49iterators (iterators that meet the requirements for one of the
50iterator concepts in the current standard). In the case of old
51iterators, the writability and swapability is deduced based on the
52<tt class="literal"><span class="pre">iterator_category</span></tt> and also the <tt class="literal"><span class="pre">reference</span></tt> type. The
53specification for this deduction gives false positives for forward
54iterators that have non-assignable value types.</p>
55<p>To review, the part of the <tt class="literal"><span class="pre">is_writable</span></tt> trait definition which
56applies to old iterators is:</p>
57<pre class="literal-block">
58if (cat is convertible to output_iterator_tag)
59    return true;
60else if (cat is convertible to forward_iterator_tag
61         and iterator_traits&lt;Iterator&gt;::reference is a
62             mutable reference)
63    return true;
64else
65    return false;
66</pre>
67<p>Suppose the <tt class="literal"><span class="pre">value_type</span></tt> of the iterator <tt class="literal"><span class="pre">It</span></tt> has a private
68assignment operator:</p>
69<pre class="literal-block">
70class B {
71public:
72  ...
73private:
74  B&amp; operator=(const B&amp;);
75};
76</pre>
77<p>and suppose the <tt class="literal"><span class="pre">reference</span></tt> type of the iterator is <tt class="literal"><span class="pre">B&amp;</span></tt>.  In
78that case, <tt class="literal"><span class="pre">is_writable&lt;It&gt;::value</span></tt> will be true when in fact
79attempting to write into <tt class="literal"><span class="pre">B</span></tt> will cause an error.</p>
80<p>The same problem applies to <tt class="literal"><span class="pre">is_swappable</span></tt>.</p>
81</div>
82<div class="section" id="proposed-resolution">
83<h1><a class="toc-backref" href="#id2" name="proposed-resolution">Proposed Resolution</a></h1>
84<ol class="arabic">
85<li><p class="first">Remove the <tt class="literal"><span class="pre">is_writable</span></tt> and <tt class="literal"><span class="pre">is_swappable</span></tt> traits, and remove the
86requirements in the Writable Iterator and Swappable Iterator concepts
87that require their models to support these traits.</p>
88</li>
89<li><p class="first">Change the <tt class="literal"><span class="pre">is_readable</span></tt> specification to be:
90<tt class="literal"><span class="pre">is_readable&lt;X&gt;::type</span></tt> is <tt class="literal"><span class="pre">true_type</span></tt> if the
91result type of <tt class="literal"><span class="pre">X::operator*</span></tt> is convertible to
92<tt class="literal"><span class="pre">iterator_traits&lt;X&gt;::value_type</span></tt> and is <tt class="literal"><span class="pre">false_type</span></tt>
93otherwise. Also, <tt class="literal"><span class="pre">is_readable</span></tt> is required to satisfy
94the requirements for the UnaryTypeTrait concept
95(defined in the type traits proposal).</p>
96<p>Remove the requirement for support of the <tt class="literal"><span class="pre">is_readable</span></tt> trait from
97the Readable Iterator concept.</p>
98</li>
99<li><p class="first">Remove the <tt class="literal"><span class="pre">iterator_tag</span></tt> class.</p>
100</li>
101<li><p class="first">Change the specification of <tt class="literal"><span class="pre">traversal_category</span></tt> to:</p>
102<pre class="literal-block">
103traversal-category(Iterator) =
104    let cat = iterator_traits&lt;Iterator&gt;::iterator_category
105    if (cat is convertible to incrementable_iterator_tag)
106      return cat; // Iterator is a new iterator
107    else if (cat is convertible to random_access_iterator_tag)
108        return random_access_traversal_tag;
109    else if (cat is convertible to bidirectional_iterator_tag)
110        return bidirectional_traversal_tag;
111    else if (cat is convertible to forward_iterator_tag)
112        return forward_traversal_tag;
113    else if (cat is convertible to input_iterator_tag)
114        return single_pass_iterator_tag;
115    else if (cat is convertible to output_iterator_tag)
116        return incrementable_iterator_tag;
117    else
118        return null_category_tag;
119</pre>
120</li>
121</ol>
122</div>
123<div class="section" id="rationale">
124<h1><a class="toc-backref" href="#id3" name="rationale">Rationale</a></h1>
125<ol class="arabic simple">
126<li>There are two reasons for removing <tt class="literal"><span class="pre">is_writable</span></tt>
127and <tt class="literal"><span class="pre">is_swappable</span></tt>. The first is that we do not know of
128a way to fix the specification so that it gives the correct
129answer for all iterators. Second, there was only a weak
130motivation for having <tt class="literal"><span class="pre">is_writable</span></tt> and <tt class="literal"><span class="pre">is_swappable</span></tt>
131there in the first place.  The main motivation was simply
132uniformity: we have tags for the old iterator categories
133so we should have tags for the new iterator categories.
134While having tags and the capability to dispatch based
135on the traversal categories is often used, we see
136less of a need for dispatching based on writability
137and swappability, since typically algorithms
138that need these capabilities have no alternative if
139they are not provided.</li>
140<li>We discovered that the <tt class="literal"><span class="pre">is_readable</span></tt> trait can be implemented
141using only the iterator type itself and its <tt class="literal"><span class="pre">value_type</span></tt>.
142Therefore we remove the requirement for <tt class="literal"><span class="pre">is_readable</span></tt> from the
143Readable Iterator concept, and change the definition of
144<tt class="literal"><span class="pre">is_readable</span></tt> so that it works for any iterator type.</li>
145<li>The purpose of the <tt class="literal"><span class="pre">iterator_tag</span></tt> class was to
146bundle the traversal and access category tags
147into the <tt class="literal"><span class="pre">iterator_category</span></tt> typedef.
148With <tt class="literal"><span class="pre">is_writable</span></tt> and <tt class="literal"><span class="pre">is_swappable</span></tt> gone, and
149<tt class="literal"><span class="pre">is_readable</span></tt> no longer in need of special hints,
150there is no reason for iterators to provide
151information about the access capabilities of an iterator.
152Thus there is no need for the <tt class="literal"><span class="pre">iterator_tag</span></tt>. The
153traversal tag can be directly used for the
154<tt class="literal"><span class="pre">iterator_category</span></tt>. If a new iterator is intended to be backward
155compatible with old iterator concepts, a tag type
156that is convertible to both one of the new traversal tags
157and also to an old iterator tag can be created and use
158for the <tt class="literal"><span class="pre">iterator_category</span></tt>.</li>
159<li>The changes to the specification of <tt class="literal"><span class="pre">traversal_category</span></tt> are a
160direct result of the removal of <tt class="literal"><span class="pre">iterator_tag</span></tt>.</li>
161</ol>
162</div>
163</div>
164<hr class="footer" />
165<div class="footer">
166<a class="reference" href="issues.rst">View document source</a>.
167Generated by <a class="reference" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
168</div>
169</body>
170</html>
Note: See TracBrowser for help on using the repository browser.