std::filesy...ry_iterator)(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌸 std::filesystem::begin(directory_iterator) / std::filesystem::end(directory_iterator) 🌸

okay so this is the C++ Standard Libary manual page β€” yes, that's literally how the header spells it, and we are not fixing it, we are respecting the typo πŸ’… β€” and it's all about the begin / end bestie duo for directory_iterator. periodt.


πŸŽ€ NAME πŸŽ€

std::filesystem::begin(directory_iterator),std::filesystem::end(directory_iterator) β€” and the NAME section literally repeats it: std::filesystem::begin(directory_iterator),std::filesystem::end(directory_iterator). so that's the whole name vibe, no notes πŸ‘‘


✨ Synopsis ✨

Defined in header <filesystem>
directory_iterator begin( directory_iterator iter ) noexcept; (1)
(since C++17)
directory_iterator end( directory_iterator ) noexcept;        (2)
(since C++17)
  1. Returns iter unchanged.
  2. Returns a default-constructed directory_iterator, which serves as the end iterator. The argument is ignored.

These non-member functions enable the use of directory_iterators with range-based for loops and make directory_iterator a range type (since C++20). okay slay, that's the whole point 🫢


🌈 Parameters 🌈

  • iter β€” a directory_iterator

πŸ’– Return value πŸ’–

  1. iter unchanged.
  2. End iterator (default-constructed directory_iterator).

🧚 Example 🧚

// Run this code

#include <filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::filesystem;

int main()
{
    fs::create_directories("sandbox/a/b");
    std::ofstream("sandbox/file1.txt");
    std::ofstream("sandbox/file2.txt");
    for (auto& p : fs::directory_iterator("sandbox"))
        std::cout << p << '\n';
    fs::remove_all("sandbox");
}

πŸ–₯️ Possible output πŸ–₯️

"sandbox/a"
"sandbox/file1.txt"
"sandbox/file2.txt"

🩹 Defect reports 🩹

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • DR LWG 3480 β€” Applied to C++17; behavior as published: end took the argument by reference; correct behavior: takes the argument by value.

πŸ‘€ See also πŸ‘€

  • begin(std::filesystem::recursive_directory_iterator) β€” range-based for loop support
  • end(std::filesystem::recursive_directory_iterator) β€” (function)

okay that's the whole page, bestie 🫢 β€” source/version: http://cppreference.com, date: 2024.06.10, and the footer tag is std::filesy...ry_iterator)(3). mwah πŸ’‹

πŸ“¦ where this page comes from