std::filesy...s_directory(3)
π 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:
std::filesystem::is_directory(status()),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. π
- Throws
std::filesystem::filesystem_erroron underlying OS API errors, constructed withpas the first path argument and the OS error code as the error code argument. - Sets a
std::error_code¶meter to the OS API error code if an OS API call fails, and executesec.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 thatd3is out here lying until you callrefresh()on it π cached results are real, bestie β thedirectory_entrydoesn'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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms