mirror of
https://github.com/boostorg/iterator.git
synced 2025-07-20 08:02:10 +02:00
419 lines
16 KiB
HTML
Executable File
419 lines
16 KiB
HTML
Executable File
<?xml version="1.0" encoding="utf-8" ?>
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="generator" content="Docutils 0.3.1: http://docutils.sourceforge.net/" />
|
|
<title>Problem with is_writable and is_swappable in N1550</title>
|
|
<style type="text/css"><!--
|
|
|
|
/*
|
|
:Author: David Goodger
|
|
:Contact: goodger@users.sourceforge.net
|
|
:date: $Date$
|
|
:version: $Revision$
|
|
:copyright: This stylesheet has been placed in the public domain.
|
|
|
|
Default cascading style sheet for the HTML output of Docutils.
|
|
*/
|
|
|
|
.first {
|
|
margin-top: 0 }
|
|
|
|
.last {
|
|
margin-bottom: 0 }
|
|
|
|
a.toc-backref {
|
|
text-decoration: none ;
|
|
color: black }
|
|
|
|
dd {
|
|
margin-bottom: 0.5em }
|
|
|
|
div.abstract {
|
|
margin: 2em 5em }
|
|
|
|
div.abstract p.topic-title {
|
|
font-weight: bold ;
|
|
text-align: center }
|
|
|
|
div.attention, div.caution, div.danger, div.error, div.hint,
|
|
div.important, div.note, div.tip, div.warning {
|
|
margin: 2em ;
|
|
border: medium outset ;
|
|
padding: 1em }
|
|
|
|
div.attention p.admonition-title, div.caution p.admonition-title,
|
|
div.danger p.admonition-title, div.error p.admonition-title,
|
|
div.warning p.admonition-title {
|
|
color: red ;
|
|
font-weight: bold ;
|
|
font-family: sans-serif }
|
|
|
|
div.hint p.admonition-title, div.important p.admonition-title,
|
|
div.note p.admonition-title, div.tip p.admonition-title {
|
|
font-weight: bold ;
|
|
font-family: sans-serif }
|
|
|
|
div.dedication {
|
|
margin: 2em 5em ;
|
|
text-align: center ;
|
|
font-style: italic }
|
|
|
|
div.dedication p.topic-title {
|
|
font-weight: bold ;
|
|
font-style: normal }
|
|
|
|
div.figure {
|
|
margin-left: 2em }
|
|
|
|
div.footer, div.header {
|
|
font-size: smaller }
|
|
|
|
div.system-messages {
|
|
margin: 5em }
|
|
|
|
div.system-messages h1 {
|
|
color: red }
|
|
|
|
div.system-message {
|
|
border: medium outset ;
|
|
padding: 1em }
|
|
|
|
div.system-message p.system-message-title {
|
|
color: red ;
|
|
font-weight: bold }
|
|
|
|
div.topic {
|
|
margin: 2em }
|
|
|
|
h1.title {
|
|
text-align: center }
|
|
|
|
h2.subtitle {
|
|
text-align: center }
|
|
|
|
hr {
|
|
width: 75% }
|
|
|
|
ol.simple, ul.simple {
|
|
margin-bottom: 1em }
|
|
|
|
ol.arabic {
|
|
list-style: decimal }
|
|
|
|
ol.loweralpha {
|
|
list-style: lower-alpha }
|
|
|
|
ol.upperalpha {
|
|
list-style: upper-alpha }
|
|
|
|
ol.lowerroman {
|
|
list-style: lower-roman }
|
|
|
|
ol.upperroman {
|
|
list-style: upper-roman }
|
|
|
|
p.caption {
|
|
font-style: italic }
|
|
|
|
p.credits {
|
|
font-style: italic ;
|
|
font-size: smaller }
|
|
|
|
p.label {
|
|
white-space: nowrap }
|
|
|
|
p.topic-title {
|
|
font-weight: bold }
|
|
|
|
pre.address {
|
|
margin-bottom: 0 ;
|
|
margin-top: 0 ;
|
|
font-family: serif ;
|
|
font-size: 100% }
|
|
|
|
pre.line-block {
|
|
font-family: serif ;
|
|
font-size: 100% }
|
|
|
|
pre.literal-block, pre.doctest-block {
|
|
margin-left: 2em ;
|
|
margin-right: 2em ;
|
|
background-color: #eeeeee }
|
|
|
|
span.classifier {
|
|
font-family: sans-serif ;
|
|
font-style: oblique }
|
|
|
|
span.classifier-delimiter {
|
|
font-family: sans-serif ;
|
|
font-weight: bold }
|
|
|
|
span.interpreted {
|
|
font-family: sans-serif }
|
|
|
|
span.option-argument {
|
|
font-style: italic }
|
|
|
|
span.pre {
|
|
white-space: pre }
|
|
|
|
span.problematic {
|
|
color: red }
|
|
|
|
table {
|
|
margin-top: 0.5em ;
|
|
margin-bottom: 0.5em }
|
|
|
|
table.citation {
|
|
border-left: solid thin gray ;
|
|
padding-left: 0.5ex }
|
|
|
|
table.docinfo {
|
|
margin: 2em 4em }
|
|
|
|
table.footnote {
|
|
border-left: solid thin black ;
|
|
padding-left: 0.5ex }
|
|
|
|
td, th {
|
|
padding-left: 0.5em ;
|
|
padding-right: 0.5em ;
|
|
vertical-align: top }
|
|
|
|
th.docinfo-name, th.field-name {
|
|
font-weight: bold ;
|
|
text-align: left ;
|
|
white-space: nowrap }
|
|
|
|
h1 tt, h2 tt, h3 tt, h4 tt, h5 tt, h6 tt {
|
|
font-size: 100% }
|
|
|
|
tt {
|
|
background-color: #eeeeee }
|
|
|
|
ul.auto-toc {
|
|
list-style-type: none }
|
|
|
|
--></style>
|
|
</head>
|
|
<body>
|
|
<div class="document" id="problem-with-is-writable-and-is-swappable-in-n1550">
|
|
<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>
|
|
<table class="field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Author:</th><td class="field-body">David Abrahams and Jeremy Siek</td>
|
|
</tr>
|
|
<tr class="field"><th class="field-name">Contact:</th><td class="field-body"><a class="reference" href="mailto:dave@boost-consulting.com">dave@boost-consulting.com</a>, <a class="reference" href="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</a></td>
|
|
</tr>
|
|
<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>
|
|
</tr>
|
|
<tr class="field"><th class="field-name">date:</th><td class="field-body">$Date$</td>
|
|
</tr>
|
|
<tr class="field"><th class="field-name">Copyright:</th><td class="field-body">Copyright David Abrahams, Jeremy Siek 2003. Use, modification and
|
|
distribution is subject to the Boost Software License,
|
|
Version 1.0. (See accompanying file LICENSE_1_0.txt or copy
|
|
at <a class="reference" href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<div class="contents topic" id="table-of-contents">
|
|
<p class="topic-title"><a name="table-of-contents">Table of Contents</a></p>
|
|
<ul class="simple">
|
|
<li><a class="reference" href="#introduction" id="id3" name="id3">Introduction</a></li>
|
|
<li><a class="reference" href="#proposed-resolution" id="id4" name="id4">Proposed Resolution</a></li>
|
|
<li><a class="reference" href="#rationale" id="id5" name="id5">Rationale</a></li>
|
|
</ul>
|
|
</div>
|
|
<div class="section" id="introduction">
|
|
<h1><a class="toc-backref" href="#id3" name="introduction">Introduction</a></h1>
|
|
<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>
|
|
provide a mechanism for determining at compile time if an iterator
|
|
type is a model of the new Writable Iterator and Swappable Iterator
|
|
concepts, analogous to <tt class="literal"><span class="pre">iterator_traits<X>::iterator_category</span></tt>
|
|
for the old iterator concepts. For backward compatibility,
|
|
<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
|
|
iterators, but they also are intended to work for old
|
|
iterators (iterators that meet the requirements for one of the
|
|
iterator concepts in the current standard). In the case of old
|
|
iterators, the writability and swapability is deduced based on the
|
|
<tt class="literal"><span class="pre">iterator_category</span></tt> and also the <tt class="literal"><span class="pre">reference</span></tt> type. The
|
|
specification for this deduction gives false positives for forward
|
|
iterators that have non-assignable value types.</p>
|
|
<p>To review, the part of the <tt class="literal"><span class="pre">is_writable</span></tt> trait definition which
|
|
applies to old iterators is:</p>
|
|
<pre class="literal-block">
|
|
if (cat is convertible to output_iterator_tag)
|
|
return true;
|
|
else if (cat is convertible to forward_iterator_tag
|
|
and iterator_traits<Iterator>::reference is a
|
|
mutable reference)
|
|
return true;
|
|
else
|
|
return false;
|
|
</pre>
|
|
<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
|
|
assignment operator:</p>
|
|
<pre class="literal-block">
|
|
class B {
|
|
public:
|
|
...
|
|
private:
|
|
B& operator=(const B&);
|
|
};
|
|
</pre>
|
|
<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&</span></tt>. In
|
|
that case, <tt class="literal"><span class="pre">is_writable<It>::value</span></tt> will be true when in fact
|
|
attempting to write into <tt class="literal"><span class="pre">B</span></tt> will cause an error.</p>
|
|
<p>The same problem applies to <tt class="literal"><span class="pre">is_swappable</span></tt>.</p>
|
|
</div>
|
|
<div class="section" id="proposed-resolution">
|
|
<h1><a class="toc-backref" href="#id4" name="proposed-resolution">Proposed Resolution</a></h1>
|
|
<ol class="arabic">
|
|
<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
|
|
requirements in the Writable Iterator and Swappable Iterator concepts
|
|
that require their models to support these traits.</p>
|
|
</li>
|
|
<li><p class="first">Change the <tt class="literal"><span class="pre">is_readable</span></tt> specification to be:
|
|
<tt class="literal"><span class="pre">is_readable<X>::type</span></tt> is <tt class="literal"><span class="pre">true_type</span></tt> if the
|
|
result type of <tt class="literal"><span class="pre">X::operator*</span></tt> is convertible to
|
|
<tt class="literal"><span class="pre">iterator_traits<X>::value_type</span></tt> and is <tt class="literal"><span class="pre">false_type</span></tt>
|
|
otherwise.</p>
|
|
<p>Remove the requirement for support of the <tt class="literal"><span class="pre">is_readable</span></tt> trait from
|
|
the Readable Iterator concept.</p>
|
|
</li>
|
|
</ol>
|
|
<!-- We should give some attention to the UnaryTypeTrait concept,
|
|
which requires the trait to be derived from either true_type or
|
|
false_type (as of the last LWG meeting). -->
|
|
<ol class="arabic" start="3">
|
|
<li><p class="first">Change <tt class="literal"><span class="pre">iterator_tag</span></tt> to:</p>
|
|
<pre class="literal-block">
|
|
template <class Value, class Reference, class Traversal>
|
|
struct iterator_tag;
|
|
</pre>
|
|
<p>The argument for <tt class="literal"><span class="pre">Value</span></tt> must be the <tt class="literal"><span class="pre">value_type</span></tt> of the
|
|
iterator, possibly const-qualified, <tt class="literal"><span class="pre">Reference</span></tt> must be the
|
|
return type of <tt class="literal"><span class="pre">operator*</span></tt> <a class="footnote-reference" href="#id2" id="id1" name="id1"><sup>*</sup></a>, and <tt class="literal"><span class="pre">Traversal</span></tt> the
|
|
traversal tag for the iterator.</p>
|
|
</li>
|
|
</ol>
|
|
<!-- I think the language above is still too informal. There is no
|
|
"the iterator", when considering iterator_tag in isolation.
|
|
Perhaps that language belongs in a non-normative note
|
|
|
|
``iterator_tag<Value,Reference,Traversal>`` is required to be
|
|
convertible to both ``Traversal`` tag and also to the
|
|
``iterator_category`` type specified by the following
|
|
pseudo-code::
|
|
|
|
old-category(Value, Reference, Traversal) =
|
|
if (Reference is a reference
|
|
and Traversal is convertible to forward_traversal_tag)
|
|
{
|
|
if (Traversal is convertible to random_access_traversal_tag)
|
|
return random_access_iterator_tag;
|
|
else if (Traversal is convertible to bidirectional_traversal_tag)
|
|
return bidirectional_iterator_tag;
|
|
else
|
|
return forward_iterator_tag;
|
|
}
|
|
else if (Traversal is convertible to single_pass_traversal_tag
|
|
and Reference is convertible to Value)
|
|
{
|
|
if (Value is const)
|
|
return input_iterator_tag;
|
|
else
|
|
return input_output_iterator_tag;
|
|
} else
|
|
return output_iterator_tag; -->
|
|
<!-- I reformatted the code for legibility; sorry. -->
|
|
<table class="footnote" frame="void" id="id2" rules="none">
|
|
<colgroup><col class="label" /><col /></colgroup>
|
|
<tbody valign="top">
|
|
<tr><td class="label"><a class="fn-backref" href="#id1" name="id2">[*]</a></td><td>Instead of saying "return type of operator*", we could have
|
|
said <tt class="literal"><span class="pre">iterator_traits<X>::reference</span></tt>. However, the standard
|
|
specifies nothing about <tt class="literal"><span class="pre">iterator_traits<X>::reference</span></tt> in
|
|
many cases, which we believe is a defect. Furthermore, in some
|
|
cases it explicitly differs from the return type of
|
|
<tt class="literal"><span class="pre">operator*</span></tt>, for example see <tt class="literal"><span class="pre">istreambuf_iterator</span></tt>.</td></tr>
|
|
</tbody>
|
|
</table>
|
|
<ol class="arabic" start="4">
|
|
<li><p class="first">Change the specification of <tt class="literal"><span class="pre">traversal_category</span></tt> to:</p>
|
|
<pre class="literal-block">
|
|
traversal-category(Iterator) =
|
|
let cat = iterator_traits<Iterator>::iterator_category
|
|
if (cat is convertible to incrementable_iterator_tag)
|
|
return cat; // Iterator is a new iterator
|
|
else if (cat is convertible to random_access_iterator_tag)
|
|
return random_access_traversal_tag;
|
|
else if (cat is convertible to bidirectional_iterator_tag)
|
|
return bidirectional_traversal_tag;
|
|
else if (cat is convertible to forward_iterator_tag)
|
|
return forward_traversal_tag;
|
|
else if (cat is convertible to input_iterator_tag)
|
|
return single_pass_iterator_tag;
|
|
else if (cat is convertible to output_iterator_tag)
|
|
return incrementable_iterator_tag;
|
|
else
|
|
return null_category_tag;
|
|
</pre>
|
|
</li>
|
|
</ol>
|
|
</div>
|
|
<div class="section" id="rationale">
|
|
<h1><a class="toc-backref" href="#id5" name="rationale">Rationale</a></h1>
|
|
<ol class="arabic simple">
|
|
<li>There are two reasons for removing <tt class="literal"><span class="pre">is_writable</span></tt>
|
|
and <tt class="literal"><span class="pre">is_swappable</span></tt>. The first is that we do not know of
|
|
a way to fix the specification so that it gives the correct
|
|
answer for all iterators. Second, there was only a weak
|
|
motivation for having <tt class="literal"><span class="pre">is_writable</span></tt> and <tt class="literal"><span class="pre">is_swappable</span></tt>
|
|
there in the first place. The main motivation was simply
|
|
uniformity: we have tags for the old iterator categories
|
|
so we should have tags for the new iterator categories.
|
|
While having tags and the capability to dispatch based
|
|
on the traversal categories is often used, we see
|
|
less of a need for dispatching based on writability
|
|
and swappability, since typically algorithms
|
|
that need these capabilities have no alternative if
|
|
they are not provided.</li>
|
|
<li>We discovered that the <tt class="literal"><span class="pre">is_readable</span></tt> trait can be implemented
|
|
using only the iterator type itself and its <tt class="literal"><span class="pre">value_type</span></tt>.
|
|
Therefore we remove the requirement for <tt class="literal"><span class="pre">is_readable</span></tt> from the
|
|
Readable Iterator concept, and change the definition of
|
|
<tt class="literal"><span class="pre">is_readable</span></tt> so that it works for any iterator type.</li>
|
|
<li>With <tt class="literal"><span class="pre">is_writable</span></tt> and <tt class="literal"><span class="pre">is_swappable</span></tt> gone, and
|
|
<tt class="literal"><span class="pre">is_readable</span></tt> no longer in need of special hints,
|
|
there is no reason for the <tt class="literal"><span class="pre">iterator_tag</span></tt> class to provide
|
|
information about the access capabilities of an iterator.
|
|
This new version provides only information about the traversal
|
|
capabilities and the old iterator category tag. Instead of accessing
|
|
the traversal category as a nested typedef <tt class="literal"><span class="pre">::traversal</span></tt>,
|
|
the <tt class="literal"><span class="pre">iterator_tag</span></tt> itself will be convertible to the traversal
|
|
tag. The <tt class="literal"><span class="pre">access_bits</span></tt> parameter is no longer needed for
|
|
specifying the access member (which is now gone). However,
|
|
some access information is still needed so that we can
|
|
deduce the appropriate old iterator category. The
|
|
<tt class="literal"><span class="pre">Value</span></tt> and <tt class="literal"><span class="pre">Reference</span></tt> parameters fill this need.
|
|
Note that this solution cleans up the issue that John
|
|
Maddock raised on the reflector (<tt class="literal"><span class="pre">c++std-lib-12187</span></tt>) about the non-uniformity
|
|
of the lvalue bit.</li>
|
|
<li>The changes to the specification of <tt class="literal"><span class="pre">traversal_category</span></tt> are a
|
|
direct result of the changes to <tt class="literal"><span class="pre">iterator_tag</span></tt>.</li>
|
|
</ol>
|
|
</div>
|
|
</div>
|
|
<hr class="footer" />
|
|
<div class="footer">
|
|
<a class="reference" href="issues.rst">View document source</a>.
|
|
Generated on: 2003-11-17 17:15 UTC.
|
|
Generated 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.
|
|
</div>
|
|
</body>
|
|
</html>
|