<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2438: std::iterator inheritance shouldn't be mandated</title>
<meta property="og:title" content="Issue 2438: std::iterator inheritance shouldn't be mandated">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2438.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++17">C++17</a> status.</em></p>
<h3 id="2438"><a href="lwg-defects.html#2438">2438</a>. <code>std::iterator</code> inheritance shouldn't be mandated</h3>
<p><b>Section:</b> D.17 <a href="https://wg21.link/depr.iterator">[depr.iterator]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Stephan T. Lavavej <b>Opened:</b> 2014-10-01 <b>Last modified:</b> 2023-02-07</p>
<p><b>Priority: </b>3
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>
<p>
For LWG convenience, nine STL iterators are depicted as deriving from <code>std::iterator</code> to get their 
<code>iterator_category</code>/etc. typedefs. Unfortunately (and unintentionally), this also mandates the 
inheritance, which is observable (not just through <code>is_base_of</code>, but also overload resolution). 
This is unfortunate because it confuses users, who can be misled into thinking that their own iterators 
must derive from <code>std::iterator</code>, or that overloading functions to take <code>std::iterator</code> is 
somehow meaningful. This is also unintentional because the STL's most important iterators, the container 
iterators, aren't required to derive from <code>std::iterator</code>. (Some are even allowed to be raw pointers.)  
Finally, this unnecessarily constrains implementers, who may not want to derive from <code>std::iterator</code>.  
(For example, to simplify debugger views.)
<p/>
We could add wording to 99 [iterator.basic] saying that any depicted inheritance is for exposition 
only, but that wouldn't really solve reader confusion. Replacing the depicted inheritance with direct typedefs 
will prevent confusion. Note that implementers won't be required to change their code &mdash; they are free to 
continue deriving from <code>std::iterator</code> if they want.
<p/>
(Editorial note: The order of the typedefs follows the order of <code>std::iterator</code>'s template parameters.)
</p>

<p><i>[Urbana 2014-11-07: Move to Ready]</i></p>




<p id="res-2438"><b>Proposed resolution:</b></p>
<p>
This wording is relative to N3936.
</p>

<ol>
<li><p>Change  [storage.iterator], class template <code>raw_storage_iterator</code> synopsis, as depicted:</p>

<blockquote>
<pre>
template &lt;class OutputIterator, class T&gt;
class raw_storage_iterator
  <del>: public iterator&lt;output_iterator_tag,void,void,void,void&gt;</del> {
public:
  <ins>typedef output_iterator_tag iterator_category;
  typedef void value_type;
  typedef void difference_type;
  typedef void pointer;
  typedef void reference;</ins>

  explicit raw_storage_iterator(OutputIterator x);
  [&hellip;]
};
</pre>
</blockquote></li>

<li><p>Change 24.5.1.2 <a href="https://wg21.link/reverse.iterator">[reverse.iterator]</a>, class template <code>reverse_iterator</code> synopsis, as depicted
(editorial note: this reorders "<code>reference</code>, <code>pointer</code>" to "<code>pointer</code>, <code>reference</code>" 
and aligns whitespace):</p>

<blockquote>
<pre>
template &lt;class Iterator&gt;
class reverse_iterator <del>: public 
  iterator&lt;typename iterator_traits&lt;Iterator&gt;::iterator_category,
  typename iterator_traits&lt;Iterator&gt;::value_type,
  typename iterator_traits&lt;Iterator&gt;::difference_type,
  typename iterator_traits&lt;Iterator&gt;::pointer,
  typename iterator_traits&lt;Iterator&gt;::reference&gt;</del> {
public:
  <del>typedef Iterator iterator_type;
  typedef typename iterator_traits&lt;Iterator&gt;::difference_type difference_type;
  typedef typename iterator_traits&lt;Iterator&gt;::reference reference;
  typedef typename iterator_traits&lt;Iterator&gt;::pointer pointer;</del>
  <ins>typedef Iterator                                              iterator_type;
  typedef typename iterator_traits&lt;Iterator&gt;::iterator_category iterator_category;
  typedef typename iterator_traits&lt;Iterator&gt;::value_type        value_type;
  typedef typename iterator_traits&lt;Iterator&gt;::difference_type   difference_type;
  typedef typename iterator_traits&lt;Iterator&gt;::pointer           pointer;
  typedef typename iterator_traits&lt;Iterator&gt;::reference         reference;</ins>

  reverse_iterator();
  [&hellip;]
};
</pre>
</blockquote></li>

<li><p>Change 24.5.2.2 <a href="https://wg21.link/back.insert.iterator">[back.insert.iterator]</a>, class template <code>back_insert_iterator</code> synopsis, as depicted:</p>

<blockquote>
<pre>
template &lt;class Container&gt;
class back_insert_iterator <del>: 
  public iterator&lt;output_iterator_tag,void,void,void,void&gt;</del> {
protected:
  Container* container;

public:
  <ins>typedef output_iterator_tag iterator_category;
  typedef void value_type;
  typedef void difference_type;
  typedef void pointer;
  typedef void reference;</ins>
  typedef Container container_type;
  explicit back_insert_iterator(Container&amp; x);
  [&hellip;]
};
</pre>
</blockquote></li>

<li><p>Change 24.5.2.3 <a href="https://wg21.link/front.insert.iterator">[front.insert.iterator]</a>, class template <code>front_insert_iterator</code> synopsis, as depicted:</p>

<blockquote>
<pre>
template &lt;class Container&gt;
class front_insert_iterator <del>: 
  public iterator&lt;output_iterator_tag,void,void,void,void&gt;</del> {
protected:
  Container* container;

public:
  <ins>typedef output_iterator_tag iterator_category;
  typedef void value_type;
  typedef void difference_type;
  typedef void pointer;
  typedef void reference;</ins>
  typedef Container container_type;
  explicit front_insert_iterator(Container&amp; x);
  [&hellip;]
};
</pre>
</blockquote></li>

<li><p>Change 24.5.2.4 <a href="https://wg21.link/insert.iterator">[insert.iterator]</a>, class template <code>insert_iterator</code> synopsis, as depicted:</p>

<blockquote>
<pre>
template &lt;class Container&gt;
class insert_iterator <del>: 
  public iterator&lt;output_iterator_tag,void,void,void,void&gt;</del> {
protected:
  Container* container;
  typename Container::iterator iter;

public:
  <ins>typedef output_iterator_tag iterator_category;
  typedef void value_type;
  typedef void difference_type;
  typedef void pointer;
  typedef void reference;</ins>
  typedef Container container_type;
  insert_iterator(Container&amp; x, typename Container::iterator i);
  [&hellip;]
};
</pre>
</blockquote></li>

<li><p>Change 24.6.2 <a href="https://wg21.link/istream.iterator">[istream.iterator]</a>, class template <code>istream_iterator</code> synopsis, as depicted:</p>

<blockquote>
<pre>
template &lt;class T, class charT = char, class traits = char_traits&lt;charT&gt;,
  class Distance = ptrdiff_t&gt;
class istream_iterator <del>: 
  public iterator&lt;input_iterator_tag, T, Distance, const T*, const T&amp;&gt;</del> {
public:
  <ins>typedef input_iterator_tag iterator_category;
  typedef T value_type;
  typedef Distance difference_type;
  typedef const T* pointer;
  typedef const T&amp; reference;</ins>
  [&hellip;]
};
</pre>
</blockquote></li>

<li><p>Change 24.6.3 <a href="https://wg21.link/ostream.iterator">[ostream.iterator]</a>, class template <code>ostream_iterator</code> synopsis, as depicted:</p>

<blockquote>
<pre>
template &lt;class T, class charT = char, class traits = char_traits&lt;charT&gt;&gt;
class ostream_iterator <del>: 
  public iterator&lt;output_iterator_tag, void, void, void, void&gt;</del> {
public:
  <ins>typedef output_iterator_tag iterator_category;
  typedef void value_type;
  typedef void difference_type;
  typedef void pointer;
  typedef void reference;</ins>
  [&hellip;]
};
</pre>
</blockquote></li>

<li><p>Change 24.6.4 <a href="https://wg21.link/istreambuf.iterator">[istreambuf.iterator]</a>, class template <code>istreambuf_iterator</code> synopsis, as depicted:</p>

<blockquote>
<pre>
template &lt;class charT = char, class traits = char_traits&lt;charT&gt; &gt;
class istreambuf_iterator <del>: 
  public iterator&lt;input_iterator_tag, charT,
                  typename traits::off_type, <em>unspecified</em>, charT&gt;</del> {
public:
  <ins>typedef input_iterator_tag iterator_category;
  typedef charT value_type;
  typedef typename traits::off_type difference_type;
  typedef <em>unspecified</em> pointer;
  typedef charT reference;</ins>
  [&hellip;]
};
</pre>
</blockquote></li>

<li><p>Change 24.6.5 <a href="https://wg21.link/ostreambuf.iterator">[ostreambuf.iterator]</a>, class template <code>ostreambuf_iterator</code> synopsis, as depicted 
(editorial note: this removes a redundant "public:"):</p>

<blockquote>
<pre>
template &lt;class charT = char, class traits = char_traits&lt;charT&gt;&gt;
class ostreambuf_iterator <del>: 
  public iterator&lt;output_iterator_tag, void, void, void, void&gt;</del> {
public:
  <ins>typedef output_iterator_tag iterator_category;
  typedef void value_type;
  typedef void difference_type;
  typedef void pointer;
  typedef void reference;</ins>
  [&hellip;]
<del>public:</del>
  [&hellip;]
};
</pre>
</blockquote></li>
</ol>






</body>
</html>
