<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2704: recursive_directory_iterator's members should require '*this is dereferenceable'</title>
<meta property="og:title" content="Issue 2704: recursive_directory_iterator's members should require '*this is dereferenceable'">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2704.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="2704"><a href="lwg-defects.html#2704">2704</a>. <code>recursive_directory_iterator</code>'s members should require '<code>*this</code> is dereferenceable'</h3>
<p><b>Section:</b> 31.12.12.2 <a href="https://wg21.link/fs.rec.dir.itr.members">[fs.rec.dir.itr.members]</a>, 31.12.11.2 <a href="https://wg21.link/fs.dir.itr.members">[fs.dir.itr.members]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Eric Fiselier <b>Opened:</b> 2016-05-08 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>1
</p>
<p><b>View all other</b> <a href="lwg-index.html#fs.rec.dir.itr.members">issues</a> in [fs.rec.dir.itr.members].</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>
In 31.12.12.2 <a href="https://wg21.link/fs.rec.dir.itr.members">[fs.rec.dir.itr.members]</a> the following members are specified as having the requirement 
"<code>*this != recursive_directory_iterator{}</code>":
</p>
<ul>
<li><p><code>options()</code></p></li>
<li><p><code>depth()</code></p></li>
<li><p><code>recursion_pending()</code></p></li>
<li><p><code>operator++</code></p></li>
<li><p><code>increment(...)</code></p></li>
<li><p><code>pop()</code></p></li>
<li><p><code>disable_recursion_pending()</code></p></li>
</ul>
<p>
This requirement is not strong enough since it still allows non-dereferenceable iterators to invoke these methods. 
For example:
</p>
<blockquote><pre>
recursive_directory_iterator it(".");
recursive_directory_iterator it_copy(it);
assert(it_copy.depth() == 0); // OK
++it;
assert(it_copy.depth() == ???); // Not OK
auto x = *it_copy; // Is this OK?
</pre></blockquote>
<p>
I believe these should instead require that <code>*this</code> is dereferenceable, however the current specification 
seems to say that all previous copies of <code>it</code> are still dereferenceable although not what they dereference to.
<p/>
[fs.class.directory_iterator] p4:
</p>
<blockquote class="note">
<p>
The result of <code>operator*</code> on an end iterator is undefined behavior. For any other iterator value a <code>const 
recursive_directory_entry&amp;</code> is returned. The result of <code>operator-&gt;</code> on an end iterator is 
undefined behavior. For any other iterator value a <code>const directory_entry*</code> is returned.
</p>
</blockquote>
<p>
Is the intention of this clause to make all non-end iterators dereferenceable?
<p/>
One further complication with these methods comes from the specification of <code>recursive_directory_iterator</code>'s 
copy/move constructors and assignment operators which specify the following post conditions:
</p>
<ul>
<li><p><code>this-&gt;options() == rhs.options()</code></p></li>
<li><p><code>this-&gt;depth() == rhs.depth()</code></p></li>
<li><p><code>this-&gt;recursion_pending() == rhs.recursion_pending()</code></p></li>
</ul>
<p>
If <code>rhs</code> is the end iterator these post conditions are poorly stated.
</p>

<p><i>[2016-06, Oulu &mdash; Daniel comments]</i></p>

<p>
The loss of information caused by bullet three of the suggested wording below is corrected by <a href="lwg-defects.html#2726" title="[recursive_]directory_iterator::increment(error_code&amp;) is underspecified (Status: C++17)">2726</a><sup><a href="https://cplusplus.github.io/LWG/issue2726" title="Latest snapshot">(i)</a></sup>'s
wording.
</p>
<p>Voted to Ready 7-0 Monday morning in Oulu</p>


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

<blockquote class="note">
<p>
[<i>Drafting note</i>: I have not attempted to fix the specification of the copy/move constructors and assignment 
operators for <code>recursive_directory_iterator</code>]</p>

<p>
[<i>Drafting note</i>: <code>increment</code> directly specifies "Effects: As specified by Input iterators (24.2.3)",
 so no additional specification is needed.]
</p>
</blockquote>

<ol>
<li><p>Change  [fs.class.directory_iterator] p4 as indicated:</p>


<blockquote><p>
-4- <del>The result of <code>operator*</code> on an end iterator is undefined behavior. For any other iterator value a <code>const
directory_entry&amp;</code> is returned. The result of <code>operator-&gt;</code> on an end iterator is undefined behavior. 
For any other iterator value a <code>const directory_entry*</code> is returned</del><ins>The end iterator is not 
dereferenceable</ins>.
</p></blockquote>
</li>

<li><p>Add a new bullet after the class synopsis in 31.12.12 <a href="https://wg21.link/fs.class.rec.dir.itr">[fs.class.rec.dir.itr]</a>:</p>

<blockquote>
<ins>-?- Calling <code>options</code>, <code>depth</code>, <code>recursion_pending</code>, <code>pop</code> or
<code>disable_recursion_pending</code> on an iterator that is not dereferencable results in undefined behavior.</ins>

</blockquote>
</li>

<li><p>Change 31.12.12.2 <a href="https://wg21.link/fs.rec.dir.itr.members">[fs.rec.dir.itr.members]</a> as indicated:</p>

<blockquote>
<pre>
directory_options options() const;
</pre>
<blockquote>
<p>
-17- <del><i>Requires</i>: <code>*this != recursive_directory_iterator()</code>.</del>
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
int depth() const;
</pre>
<blockquote>
<p>
-20- <del><i>Requires</i>: <code>*this != recursive_directory_iterator()</code>.</del>
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
bool recursion_pending() const;
</pre>
<blockquote>
<p>
-23- <del><i>Requires</i>: <code>*this != recursive_directory_iterator()</code>.</del>
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
recursive_directory_iterator&amp; operator++();
recursive_directory_iterator&amp; increment(error_code&amp; ec) noexcept;
</pre>
<blockquote>
<p>
-26- <del><i>Requires</i>: <code>*this != recursive_directory_iterator()</code>.</del>
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
void pop();
</pre>
<blockquote>
<p>
-30- <del><i>Requires</i>: <code>*this != recursive_directory_iterator()</code>.</del>
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
void disable_recursion_pending();
</pre>
<blockquote>
<p>
-32- <del><i>Requires</i>: <code>*this != recursive_directory_iterator()</code>.</del>
<p/>
[&hellip;]
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
