<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3013: (recursive_)directory_iterator construction and traversal should not be noexcept</title>
<meta property="og:title" content="Issue 3013: (recursive_)directory_iterator construction and traversal should not be noexcept">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3013.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++20">C++20</a> status.</em></p>
<h3 id="3013"><a href="lwg-defects.html#3013">3013</a>. <code>(recursive_)directory_iterator</code> construction and traversal should not be <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>, 31.12.13.4 <a href="https://wg21.link/fs.op.copy">[fs.op.copy]</a>, 31.12.13.20 <a href="https://wg21.link/fs.op.is.empty">[fs.op.is.empty]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Tim Song <b>Opened:</b> 2017-08-23 <b>Last modified:</b> 2023-02-07</p>
<p><b>Priority: </b>0
</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#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Constructing a <code>(recursive_)directory_iterator</code> from a <code>path</code> requires, at a minimum, initializing its 
underlying <code>directory_entry</code> object with the <code>path</code> formed from the supplied <code>path</code> and the name of the 
first entry, which requires a potentially throwing memory allocation; every implementation I've looked at also allocates 
memory to store additional data as well.
<p/>
Similarly, <code>increment()</code> needs to update the <code>path</code> stored in <code>directory_entry</code> object to refer 
to the name of the next entry, which may require a memory allocation. While it might conceivably be possible to 
postpone the update in this case until the iterator is dereferenced (the dereference operation is not <code>noexcept</code> 
due to its narrow contract), it seems highly unlikely that such an implementation is intended (not to mention that it 
would require additional synchronization as the dereference operations are const).
<p/>
This further calls into question whether the <code>error_code</code> overloads of <code>copy</code> and <code>is_empty</code>, 
whose specification uses <code>directory_iterator</code>, should be <code>noexcept</code>. There might be a case for keeping 
the <code>noexcept</code> for <code>is_empty</code>, although that would require changes in all implementations I checked 
(libstdc++, libc++, and Boost). <code>copy</code> appears to be relentlessly hostile to <code>noexcept</code>, since its 
specification forms a <code>path</code> via <code>operator/</code> in two places (bullets 4.7.4 and 4.8.2) in addition to the 
<code>directory_iterator</code> usage. The proposed resolution below removes both.
</p>

<p><i>[
2017-11-03 Moved to Tentatively Ready after 7 positive votes for P0 on c++std-lib.
]</i></p>

<p><i>[2018-3-17 Adopted in Jacksonville]</i></p>



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

<ol>
<li><p>Edit  [fs.class.directory_iterator], class <code>directory_iterator</code> synopsis, as indicated:</p>
<blockquote>
<pre>
[&hellip;]
explicit directory_iterator(const path&amp; p);
directory_iterator(const path&amp; p, directory_options options);
directory_iterator(const path&amp; p, error_code&amp; ec) <del>noexcept</del>;
directory_iterator(const path&amp; p, directory_options options,
                   error_code&amp; ec) <del>noexcept</del>;
[&hellip;]

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 p2 as indicated:</p>
<blockquote>
<pre>
explicit directory_iterator(const path&amp; p);
directory_iterator(const path&amp; p, directory_options options);
directory_iterator(const path&amp; p, error_code&amp; ec) <del>noexcept</del>;
directory_iterator(const path&amp; p, directory_options options, error_code&amp; ec) <del>noexcept</del>;
</pre>
<blockquote>
<p>
-2- <i>Effects:</i> [&hellip;]
<p/>
-3- <i>Throws:</i> As specified in 31.12.5 <a href="https://wg21.link/fs.err.report">[fs.err.report]</a>.
<p/>
-4- [<i>Note:</i> [&hellip;] &mdash; <i>end note</i>]
</p>
</blockquote>
</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 31.12.5 <a href="https://wg21.link/fs.err.report">[fs.err.report]</a>.
</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>
[&hellip;]
explicit recursive_directory_iterator(const path&amp; p);
recursive_directory_iterator(const path&amp; p, directory_options options);
recursive_directory_iterator(const path&amp; p, directory_options options,
                             error_code&amp; ec) <del>noexcept</del>;
recursive_directory_iterator(const path&amp; p, error_code&amp; ec) <del>noexcept</del>;
[&hellip;]

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 p2 as indicated:</p>
<blockquote>
<pre>
explicit recursive_directory_iterator(const path&amp; p);
recursive_directory_iterator(const path&amp; p, directory_options options);
recursive_directory_iterator(const path&amp; p, directory_options options, error_code&amp; ec) <del>noexcept</del>;
recursive_directory_iterator(const path&amp; p, error_code&amp; ec) <del>noexcept</del>;
</pre>
<blockquote>
<p>
-2- <i>Effects:</i> [&hellip;]
<p/>
-3- <i>Postconditions:</i> [&hellip;]
<p/>
-4- <i>Throws:</i> As specified in 31.12.5 <a href="https://wg21.link/fs.err.report">[fs.err.report]</a>.
<p/>
-5- [<i>Note:</i> [&hellip;] &mdash; <i>end note</i>]
<p/>
-6- [<i>Note:</i> [&hellip;] &mdash; <i>end note</i>]
</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> 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 31.12.5 <a href="https://wg21.link/fs.err.report">[fs.err.report]</a>.
</p>
</blockquote>
</blockquote>
</li>

<li><p>Edit 31.12.4 <a href="https://wg21.link/fs.filesystem.syn">[fs.filesystem.syn]</a>, header <code>&lt;filesystem&gt;</code> synopsis, as indicated:</p>
<blockquote>
<pre>
namespace std::filesystem {

  [&hellip;]

  void copy(const path&amp; from, const path&amp; to);
  void copy(const path&amp; from, const path&amp; to, error_code&amp; ec) <del>noexcept</del>;
  void copy(const path&amp; from, const path&amp; to, copy_options options);
  void copy(const path&amp; from, const path&amp; to, copy_options options,
            error_code&amp; ec) <del>noexcept</del>;

  [&hellip;]

  bool is_empty(const path&amp; p);
  bool is_empty(const path&amp; p, error_code&amp; ec) <del>noexcept</del>;
  
  [&hellip;]

}
</pre>
</blockquote>
</li>

<li><p>Edit 31.12.13.4 <a href="https://wg21.link/fs.op.copy">[fs.op.copy]</a> as indicated:</p>
<blockquote>
<pre>
void copy(const path&amp; from, const path&amp; to, error_code&amp; ec) <del>noexcept</del>;
</pre>
<blockquote>
<p>
-2- <i>Effects:</i> Equivalent to <code>copy(from, to, copy_options::none, ec)</code>.
</p>
</blockquote>
<pre>
void copy(const path&amp; from, const path&amp; to, copy_options options);
void copy(const path&amp; from, const path&amp; to, copy_options options,
          error_code&amp; ec) <del>noexcept</del>;
</pre>
<blockquote>
<p>
-3- <i>Requires:</i> [&hellip;]
<p/>
-4- <i>Effects:</i> [&hellip;]
<p/>
-5- <i>Throws:</i> [&hellip;]
<p/>
-6- <i>Remarks:</i> [&hellip;]
<p/>
-7- [<i>Example:</i> [&hellip;] &mdash; <i>end example</i>]
</p>
</blockquote>
</blockquote>
</li>


<li><p>Edit  [fs.op.is_empty] as indicated:</p>
<blockquote>
<pre>
bool is_empty(const path&amp; p);
bool is_empty(const path&amp; p, error_code&amp; ec) <del>noexcept</del>;
</pre>
<blockquote>
<p>
-1- <i>Effects:</i> [&hellip;]
<p/>
-2- <i>Throws:</i> [&hellip;]
</p>
</blockquote>
</blockquote>
</li>

</ol>




</body>
</html>
