<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2840: directory_iterator::increment is seemingly narrow-contract but marked noexcept</title>
<meta property="og:title" content="Issue 2840: directory_iterator::increment is seemingly narrow-contract but marked noexcept">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2840.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#Resolved">Resolved</a> status.</em></p>
<h3 id="2840"><a href="lwg-defects.html#2840">2840</a>. <code>directory_iterator::increment</code> is seemingly narrow-contract but marked <code>noexcept</code></h3>
<p><b>Section:</b> 31.12.11.2 <a href="https://wg21.link/fs.dir.itr.members">[fs.dir.itr.members]</a>, 31.12.12.2 <a href="https://wg21.link/fs.rec.dir.itr.members">[fs.rec.dir.itr.members]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Tim Song <b>Opened:</b> 2016-12-09 <b>Last modified:</b> 2020-09-06</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#fs.dir.itr.members">issues</a> in [fs.dir.itr.members].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<code>directory_iterator::increment</code> and <code>recursive_directory_iterator::increment</code> are specified by 
reference to the input iterator requirements, which is narrow-contract: it has a precondition that the iterator
is dereferenceable. Yet they are marked as <code>noexcept</code>.
<p/>
Either the <code>noexcept</code> (one of which was added by LWG <a href="lwg-defects.html#2637" title="[filesys.ts] [PDTS] All functions with error_code arguments should be noexcept (Status: TS)">2637</a><sup><a href="https://cplusplus.github.io/LWG/issue2637" title="Latest snapshot">(i)</a></sup>) should be removed, or the 
behavior of <code>increment</code> when given a nondereferenceable iterator should be specified.
<p/>
Currently, libstdc++ and MSVC report an error via the <code>error_code</code> argument for a nondereferenceable 
<code>directory_iterator</code>, while libc++ and <code>boost::filesystem</code> assert.
</p>
  
<p><i>[2017-01-27 Telecon]</i></p>

<p>Priority 2; there are some problems with the wording in alternative B.</p>
  
<p><i>[2018-01-24 Tim Song comments]</i></p>

<p> LWG <a href="lwg-defects.html#3013" title="(recursive_)directory_iterator construction and traversal should not be noexcept (Status: C++20)">3013</a><sup><a href="https://cplusplus.github.io/LWG/issue3013" title="Latest snapshot">(i)</a></sup> will remove this <code>noexcept</code> (for a different reason). The behavior of
  calling <code>increment</code> on a nondereferenceble iterator should still be clarified, as I was informed
  that LWG wanted it to be well-defined. </p>

<p><i>[2018-1-26 issues processing telecon]</i></p>

<p>Part A is already done by <a href="lwg-defects.html#3013" title="(recursive_)directory_iterator construction and traversal should not be noexcept (Status: C++20)">3013</a><sup><a href="https://cplusplus.github.io/LWG/issue3013" title="Latest snapshot">(i)</a></sup>.</p>
<p>Marshall to talk to Tim about Part B; it would be odd if <code>operator++</code> was undefined behaviour, but <code>increment</code> on the same iterator returned an error</p>

<p><i>[2018-8-15 Offline discussions]</i></p>

<p>Tim and Marshall recommend NAD for about Part B</p>

<p><i>[2018-08-20 Batavia Issues processing]</i></p>

<p>Status to resolved.</p>


<p id="res-2840"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/n4618">N4618</a>.</p>

<ol style="list-style-type: none">
<li><p>Alternative A (remove the <code>noexcept</code>):</p>

<ol>
<li><p>Edit  [fs.class.directory_iterator], class <code>directory_iterator</code> synopsis, as indicated:</p>
<blockquote>
<pre>
directory_iterator&amp; operator++();
directory_iterator&amp; increment(error_code&amp; ec) <del>noexcept</del>;
</pre>
</blockquote>
</li>

<li><p>Edit 31.12.11.2 <a href="https://wg21.link/fs.dir.itr.members">[fs.dir.itr.members]</a> before p10 as indicated:</p>
<blockquote>
<pre>
directory_iterator&amp; operator++();
directory_iterator&amp; increment(error_code&amp; ec) <del>noexcept</del>;
</pre>
<blockquote>
<p>
-10- <i>Effects:</i> As specified for the prefix increment operation of Input iterators (24.2.3).
<p/>
-11- <i>Returns:</i> <code>*this</code>.
<p/>
-12- <i>Throws:</i> As specified in 27.10.7.
</p>
</blockquote>
</blockquote>
</li>

<li><p>Edit 31.12.12 <a href="https://wg21.link/fs.class.rec.dir.itr">[fs.class.rec.dir.itr]</a>, class <code>recursive_directory_iterator</code> synopsis, as indicated:</p>
<blockquote>
<pre>
recursive_directory_iterator&amp; operator++();
recursive_directory_iterator&amp; increment(error_code&amp; ec) <del>noexcept</del>;
</pre>
</blockquote>
</li>

<li><p>Edit 31.12.12.2 <a href="https://wg21.link/fs.rec.dir.itr.members">[fs.rec.dir.itr.members]</a> before p23 as indicated:</p>
<blockquote>
<pre>
recursive_directory_iterator&amp; operator++();
recursive_directory_iterator&amp; increment(error_code&amp; ec) <del>noexcept</del>;
</pre>
<blockquote>
<p>
-23- <i>Effects:</i> As specified for the prefix increment operation of Input iterators (24.2.3), except that: [&hellip;]
<p/>
-24- <i>Returns:</i> <code>*this</code>.
<p/>
-25- <i>Throws:</i> As specified in 27.10.7.
</p>
</blockquote>
</blockquote>
</li>
</ol>
</li>

<li><p>Alternative B (specify that <code>increment</code> reports an error if the iterator is not dereferenceable):</p>

<ol>
<li><p>Edit 31.12.11.2 <a href="https://wg21.link/fs.dir.itr.members">[fs.dir.itr.members]</a> p10 as indicated:</p>
<blockquote>
<pre>
directory_iterator&amp; operator++();
directory_iterator&amp; increment(error_code&amp; ec) noexcept;
</pre>
<blockquote>
<p>
-10- <i>Effects:</i> As specified for the prefix increment operation of Input iterators (24.2.3)<ins>, except that 
<code>increment</code> reports an error if <code>*this</code> is not dereferenceable</ins>.
<p/>
-11- <i>Returns:</i> <code>*this</code>.
<p/>
-12- <i>Throws:</i> As specified in 27.10.7.
</p>
</blockquote>
</blockquote>
</li>

<li><p>Edit 31.12.12.2 <a href="https://wg21.link/fs.rec.dir.itr.members">[fs.rec.dir.itr.members]</a> p23 as indicated:</p>
<blockquote>
<pre>
recursive_directory_iterator&amp; operator++();
recursive_directory_iterator&amp; increment(error_code&amp; ec) noexcept;
</pre>
<blockquote>
<p>
-23- <i>Effects:</i> As specified for the prefix increment operation of Input iterators (24.2.3), except that: 
</p>
<ul>
<li><p><ins><code>increment</code> reports an error if <code>*this</code> is not dereferenceable.</ins></p></li>
<li><p>If there are no more entries at the current depth, [&hellip;]</p></li>
<li><p>Otherwise if [&hellip;]</p></li>
</ul>
<p>
-24- <i>Returns:</i> <code>*this</code>.
<p/>
-25- <i>Throws:</i> As specified in 27.10.7.
</p>
</blockquote>
</blockquote>
</li>
</ol>
</li>
</ol>







</body>
</html>
