<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3480: directory_iterator and recursive_directory_iterator are not C++20 ranges</title>
<meta property="og:title" content="Issue 3480: directory_iterator and recursive_directory_iterator are not C++20 ranges">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3480.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++23">C++23</a> status.</em></p>
<h3 id="3480"><a href="lwg-defects.html#3480">3480</a>. <code>directory_iterator</code> and <code>recursive_directory_iterator</code> are not C++20 ranges</h3>
<p><b>Section:</b> 31.12.11 <a href="https://wg21.link/fs.class.directory.iterator">[fs.class.directory.iterator]</a>, 31.12.12 <a href="https://wg21.link/fs.class.rec.dir.itr">[fs.class.rec.dir.itr]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Barry Revzin <b>Opened:</b> 2020-08-27 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#fs.class.directory.iterator">issues</a> in [fs.class.directory.iterator].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++23">C++23</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<code>std::filesystem::directory_iterator</code> and <code>std::filesystem::recursive_directory_iterator</code> are
intended to be ranges, but both fail to satisfy the concept <code>std::ranges::range</code>.
<p/>
They both opt in to being a range the same way, via non-member functions:
</p>
<blockquote><pre>
directory_iterator begin(directory_iterator iter) noexcept;
directory_iterator end(const directory_iterator&amp;) noexcept;

recursive_directory_iterator begin(recursive_directory_iterator iter) noexcept;
recursive_directory_iterator end(const recursive_directory_iterator&amp;) noexcept;
</pre></blockquote>
<p>
This is good enough for a range-based for statement, but for the <code>range</code> concept, non-member
<code>end</code> is looked up in a context that includes (25.3.3 <a href="https://wg21.link/range.access.end">[range.access.end]</a>/2.6) the declarations:
</p>
<blockquote><pre>
void end(auto&amp;) = delete;
void end(const auto&amp;) = delete;
</pre></blockquote>
<p>
Which means that non-<code>const</code> <code>directory_iterator</code> and non-<code>const</code>
<code>recursive_directory_iterator</code>, the <code>void end(auto&amp;)</code> overload ends up being
a better match and thus the CPO <code>ranges::end</code> doesn't find a candidate. Which means that
<code>{recursive_,}directory_iterator</code> is not a range, even though <code>const {recursive_,}directory_iterator</code>
<em>is</em> a range.
<p/>
This could be fixed by having the non-member <code>end</code> for both of these types just take by value
(as libstdc++ currently does anyway) or by adding member functions <code>begin() const</code> and <code>end() const</code>.
<p/>
A broader direction would be to consider removing the poison pill overloads. Their motivation from
<a href="https://wg21.link/p0970">P0970</a> was to support what are now called borrowed ranges &mdash; but
that design now is based on specializing a variable template instead of providing a non-member <code>begin</code>
that takes an rvalue, so the initial motivation simply no longer exists. And, in this particular case,
causes harm.
</p>

<p><i>[2020-09-06; Reflector prioritization]</i></p>

<p>
Set priority to 3 during reflector discussions.
</p>

<p><i>[2021-02-22, Barry Revzin comments]</i></p>

<p>
When we do make whichever of the alternative adjustments necessary such that
<code>range&lt;directory_iterator&gt;</code> is <code>true</code>, we should also remember
to specialize <code>enable_borrowed_range</code> for both types to be <code>true</code> (since
the iterator is the range, this is kind of trivially true).
</p>

<p><i>[2021-05-17, Tim provides wording]</i></p>

<p>
Both MSVC and libstdc++'s <code>end</code> already take its argument by value, so
the wording below just does that. Any discussion about changing or removing the
poison pills is probably better suited for a paper.
</p>

<p><i>[2021-06-23; Reflector poll]</i></p>

<p>
Set status to Tentatively Ready after seven votes in favour during reflector poll.
</p>

<p><i>[2021-10-14 Approved at October 2021 virtual plenary. Status changed: Voting &rarr; WP.]</i></p>



<p id="res-3480"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/n4885">N4885</a>.</p>
<ol>
<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>
[&hellip;]
namespace std::filesystem {
  [&hellip;]

  // 31.12.11.3 <a href="https://wg21.link/fs.dir.itr.nonmembers">[fs.dir.itr.nonmembers]</a>, range access for directory iterators
  directory_iterator begin(directory_iterator iter) noexcept;
  directory_iterator end(<del>const</del> directory_iterator<del>&amp;</del>) noexcept;

  [&hellip;]

  // 31.12.12.3 <a href="https://wg21.link/fs.rec.dir.itr.nonmembers">[fs.rec.dir.itr.nonmembers]</a>, range access for recursive directory iterators
  recursive_directory_iterator begin(recursive_directory_iterator iter) noexcept;
  recursive_directory_iterator end(<del>const</del> recursive_directory_iterator<del>&amp;</del>) noexcept;

  [&hellip;]
}

<ins>
namespace std::ranges {
  template&lt;&gt;
  inline constexpr bool enable_borrowed_range&lt;filesystem::directory_iterator&gt; = true;
  template&lt;&gt;
  inline constexpr bool enable_borrowed_range&lt;filesystem::recursive_directory_iterator&gt; = true;

  template&lt;&gt;
  inline constexpr bool enable_view&lt;filesystem::directory_iterator&gt; = true;
  template&lt;&gt;
  inline constexpr bool enable_view&lt;filesystem::recursive_directory_iterator&gt; = true;
}
</ins>
</pre>
</blockquote>
</li>
<li>
<p>Edit 31.12.11.3 <a href="https://wg21.link/fs.dir.itr.nonmembers">[fs.dir.itr.nonmembers]</a> as indicated:</p>
<blockquote>
<p>
-1- These functions enable range access for <code>directory_iterator</code>.
</p>
<pre>
directory_iterator begin(directory_iterator iter) noexcept;
</pre>
<blockquote>
<p>
-2- <i>Returns</i>: <code>iter</code>.
</p>
</blockquote>
<pre>
directory_iterator end(<del>const</del> directory_iterator<del>&amp;</del>) noexcept;
</pre>
<blockquote>
<p>
-3- <i>Returns</i>: <code>directory_iterator()</code>.
</p>
</blockquote>
</blockquote>
</li>
<li>
<p>Edit 31.12.12.3 <a href="https://wg21.link/fs.rec.dir.itr.nonmembers">[fs.rec.dir.itr.nonmembers]</a> as indicated:</p>
<blockquote>
<p>
-1- These functions enable use of <code>recursive_directory_iterator</code> with range-based for statements.
</p>
<pre>
recursive_directory_iterator begin(recursive_directory_iterator iter) noexcept;
</pre>
<blockquote>
<p>
-2- <i>Returns</i>: <code>iter</code>.
</p>
</blockquote>
<pre>
recursive_directory_iterator end(<del>const</del> recursive_directory_iterator<del>&amp;</del>) noexcept;
</pre>
<blockquote>
<p>
-3- <i>Returns</i>: <code>recursive_directory_iterator()</code>.
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
