[fs.class.directory.iterator.general]

# 31 Input/output library [[input.output]](./#input.output)

## 31.12 File systems [[filesystems]](filesystems#fs.class.directory.iterator.general)

### 31.12.11 Class directory_iterator [[fs.class.directory.iterator]](fs.class.directory.iterator#general)

#### 31.12.11.1 General [fs.class.directory.iterator.general]

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L16540)

An object of type directory_iterator provides an iterator for a
sequence of directory_entry elements representing the
path and any cached attribute values ([[fs.class.directory.entry]](fs.class.directory.entry "31.12.10 Class directory_­entry"))
for each file in a directory
or in an implementation-defined directory-like file type[.](#1.sentence-1)

[*Note [1](#note-1)*:

For iteration into subdirectories, see class recursive_directory_iterator ([[fs.class.rec.dir.itr]](fs.class.rec.dir.itr "31.12.12 Class recursive_­directory_­iterator"))[.](#1.sentence-2)

â *end note*]

namespace std::filesystem {class directory_iterator {public:using iterator_category = input_iterator_tag; using value_type = directory_entry; using difference_type = ptrdiff_t; using pointer = const directory_entry*; using reference = const directory_entry&; // [[fs.dir.itr.members]](fs.dir.itr.members "31.12.11.2 Members"), member functions directory_iterator() noexcept; explicit directory_iterator(const path& p);
 directory_iterator(const path& p, directory_options options);
 directory_iterator(const path& p, error_code& ec);
 directory_iterator(const path& p, directory_options options,
 error_code& ec);
 directory_iterator(const directory_iterator& rhs);
 directory_iterator(directory_iterator&& rhs) noexcept; ~directory_iterator();

 directory_iterator& operator=(const directory_iterator& rhs);
 directory_iterator& operator=(directory_iterator&& rhs) noexcept; const directory_entry& operator*() const; const directory_entry* operator->() const;
 directory_iterator& operator++();
 directory_iterator& increment(error_code& ec); bool operator==(default_sentinel_t) const noexcept {return *this == directory_iterator(); }// other members as required by [[input.iterators]](input.iterators "24.3.5.3 Input iterators"), input iterators};}

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L16588)

directory_iterator meets the[*Cpp17InputIterator*](input.iterators#:Cpp17InputIterator "24.3.5.3 Input iterators [input.iterators]") requirements ([[input.iterators]](input.iterators "24.3.5.3 Input iterators"))[.](#2.sentence-1)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L16592)

If an iterator of type directory_iterator reports an error or
is advanced past the last directory element,
that iterator shall become equal to the end iterator
value[.](#3.sentence-1)

The directory_iterator default constructor shall
create an iterator equal to the end iterator value, and this shall be the only
valid iterator for the end condition[.](#3.sentence-2)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L16600)

The end iterator is not dereferenceable[.](#4.sentence-1)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L16603)

Two end iterators are always equal[.](#5.sentence-1)

An end iterator shall not be equal to a non-end
iterator[.](#5.sentence-2)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L16607)

The result of calling the path() member of the directory_entry object obtained by dereferencing a directory_iterator is a reference to a path object composed of the directory argument from which the iterator was
constructed with the filename of the directory entry appended as if by operator/=[.](#6.sentence-1)

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L16611)

Directory iteration shall not yield directory entries for the current (dot)
and parent (dot-dot) directories[.](#7.sentence-1)

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L16615)

The order of directory entries obtained by dereferencing successive
increments of a directory_iterator is unspecified[.](#8.sentence-1)

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L16619)

Constructors and non-const directory_iterator member functions
store the values of any cached attributes ([[fs.class.directory.entry]](fs.class.directory.entry "31.12.10 Class directory_­entry"))
in the directory_entry element returned by operator*()[.](#9.sentence-1)

directory_iterator member functions shall not directly or indirectly call
any directory_entry refresh function[.](#9.sentence-2)

[*Note [2](#note-2)*:

The exact mechanism for storing cached attribute values is not exposed to users[.](#9.sentence-3)

â *end note*]

[10](#10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L16629)

[*Note [3](#note-3)*:

A path obtained by dereferencing a directory iterator might not actually exist;
it could be a symbolic link to a non-existent file[.](#10.sentence-1)

Recursively walking directory trees
for purposes of removing and renaming entries
might invalidate symbolic links that are being followed[.](#10.sentence-2)

â *end note*]

[11](#11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L16638)

[*Note [4](#note-4)*:

If a file is removed from or added to a directory after the
construction of a directory_iterator for the directory, it is
unspecified whether or not subsequently incrementing the iterator will ever
result in an iterator referencing the removed or added directory entry[.](#11.sentence-1)

See
POSIX [readdir](http://pubs.opengroup.org/onlinepubs/9699919799/functions/readdir.html)[.](#11.sentence-2)

â *end note*]
