I need to traverse a directory tree in the depth-first (DFS) manner. It looks like std::recursive_directory_itereator
follows that ordering on my machine, but I could not find any guarantees on that in the standard or anywhere else. Is the ordering of the entry visits guaranteed? Or even "stable" (guaranteed to be the same if, without any changes to the inspected directory tree, we execute the same code twice)?
In case where it was / wasn't specified, but that changed over the past, it would be great to know about those changes.
The visit order is not guaranteed in any way.
From std::filesystem::recursive_directory_iterator
:
The iteration order is unspecified, except that each directory entry is visited only once.
(emphasis is mine)
I assume that on a specific system the order will be stable (just an educated guess), but even that is not guaranteed.
Update:
There is a point in the specifications that is somewhat inconsistent regarding this issue:
Despite the quote above, and a similar phrasing in the draft standard, there is the info mentioned in the other answer by @HolyBlackCat, which implies that the implementation can in fact be only in a DFS manner (with the order in each directory still unspecified).