std::filesy...s_directory(3)

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

πŸ’… std::filesystem::directory_entry::is_directory πŸ’…

okay so bestie, welcome to the std::filesystem::directory_entry::is_directory page β€” it lives in the C++ Standard Library, and it's honestly kind of iconic. section three of the manual, no cap. let's get into it πŸŽ€


✨ NAME ✨

std::filesystem::directory_entry::is_directory β€” literally the member function std::filesystem::directory_entry::is_directory, that's the whole name, iykyk πŸ¦‹


🌸 SYNOPSIS 🌸

bool is_directory() const;                               (1) (since C++17)
bool is_directory( std::error_code& ec ) const noexcept; (2) (since C++17)

so what does it do? it checks whether the pointed-to object is a directory. that's it, that's the vibe πŸ’… and effectively it returns:

  1. std::filesystem::is_directory(status()),
  2. std::filesystem::is_directory(status(ec)).

like it's giving "delegate to the free function", periodt ✨


🧚 PARAMETERS 🧚

  • ec β€” out-parameter for error reporting in the non-throwing overload. that's its whole job, bestie 🫢

πŸ‘‘ RETURN VALUE πŸ‘‘

true if the referred-to filesystem object is a directory, false otherwise. iconic boolean behaviour, no notes 🌈


πŸ”₯ EXCEPTIONS πŸ”₯

any overload not marked noexcept may throw std::bad_alloc if memory allocation fails. πŸ’€

  1. Throws std::filesystem::filesystem_error on underlying OS API errors, constructed with p as the first path argument and the OS error code as the error code argument.
  2. Sets a std::error_code& parameter to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur.
ngl, overload (1) is the dramatic one that will just throw at you. overload (2) is the chill one β€” it hands you the error in ec and keeps it moving. pick your fighter πŸ’€

πŸ“ EXAMPLE πŸ“

// Run this code

#include <filesystem>
#include <iostream>
#include <string_view>

namespace fs = std::filesystem;

void check_directory(fs::directory_entry const& d, std::string_view rem = "")
{
    std::cout << "is_directory(" << d << "): " << d.is_directory() << rem << '\n';
}

int main()
{
    fs::directory_entry d1(".");
    fs::directory_entry d2("file.txt");
    fs::directory_entry d3("new_dir");

    std::cout << std::boolalpha;

    check_directory(d1);
    check_directory(d2);
    check_directory(d3, " (has not been created yet).");

    std::filesystem::create_directory("new_dir");

    check_directory(d3, " (before refresh).");
    d3.refresh();
    check_directory(d3, " (after refresh).");
}

⭐ Possible output ⭐

is_directory("."): true
is_directory("file.txt"): false
is_directory("new_dir"): false (has not been created yet).
is_directory("new_dir"): false (before refresh).
is_directory("new_dir"): true (after refresh).
the way that d3 is out here lying until you call refresh() on it 😭 cached results are real, bestie β€” the directory_entry doesn't re-stat the filesystem on its own, that's why the refresh is there. main character energy fr fr πŸ§šβ€β™€οΈ

πŸ’œ SEE ALSO πŸ’œ

  • is_directory β€” checks whether the given path refers to a directory (C++17) (function)

okay that's the whole page, mwah πŸ’‹ this one's from cppreference.com, dated 2024.06.10 β€” go off, beloved documentation, we love you, bye bestie πŸŽ€βœ¨πŸ’–

πŸ“¦ where this page comes from