std::filesy...y::is_other(3)
π std::filesystem::directory_entry::is_other(3)
okay so this page lives in the C++ Standard Library manual, section 3, and the star of the show is std::filesystem::directory_entry::is_other β the little member function that answers the question "is this thing, like, other?" π¦
β¨ SYNOPSIS β¨
bool is_other() const; (1) (since C++17)
bool is_other( std::error_code& ec ) const noexcept; (2) (since C++17)
So bestie, what does it actually do? It checks whether the pointed-to object is an other file β and by "other" we mean not a regular file, not a directory, and not a symlink. That's the whole vibe check. π
Effectively it just returns:
std::filesystem::is_other(status()).std::filesystem::is_other(status(ec)).
no cap, that's literally it. π
πΈ PARAMETERS πΈ
ecβ the out-parameter for error reporting in the non-throwing overload. she's the one who catches the drama so you don't have to π
π RETURN VALUE π
true if the referred-to filesystem object is an other file, false otherwise. simple as that, periodt. β¨
π EXCEPTIONS π
Any overload not marked noexcept may throw std::bad_alloc if memory allocation fails. (yes, even the boring allocation problems get a cameo π)
- 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. so chill. so noexcept. we love her. π«Ά
π EXAMPLE π
// Run this code
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <functional>
#include <iostream>
#include <memory>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
namespace fs = std::filesystem;
void print_entry_type(const std::filesystem::directory_entry& entry)
{
std::cout << entry.path() << ": ";
if (!entry.exists())
std::cout << "does not exist ";
if (entry.is_block_file())
std::cout << "is a block device ";
if (entry.is_character_file())
std::cout << "is a character device ";
if (entry.is_directory())
std::cout << "is a directory ";
if (entry.is_fifo())
std::cout << "is a named IPC pipe ";
if (entry.is_regular_file())
std::cout << "is a regular file ";
if (entry.is_socket())
std::cout << "is a named IPC socket ";
if (entry.is_symlink())
std::cout << "(a symlink)";
if (entry.is_other())
std::cout << "(an `other` file)";
std::cout << '\n';
}
template<typename Type, typename Fun>
class scoped_cleanup
{
std::unique_ptr<Type, std::function<void(const Type*)>> u;
public:
scoped_cleanup(Type* ptr, Fun fun) : u{ptr, std::move(fun)} {}
};
int main()
{
// Create files of different kinds.
std::filesystem::current_path(fs::temp_directory_path());
const std::filesystem::path sandbox{"sandbox"};
scoped_cleanup remove_all_at_exit{&sandbox, [](const fs::path* p)
{
std::cout << "cleanup: remove_all(" << *p << ")\n";
fs::remove_all(*p);
}};
std::filesystem::create_directory(sandbox);
std::ofstream{sandbox/"file"}; // Creates a regular file
std::filesystem::create_directory(sandbox/"dir");
mkfifo((sandbox/"pipe").string().data(), 0644);
struct sockaddr_un addr; addr.sun_family = AF_UNIX;
std::strcpy(addr.sun_path, (sandbox/"sock").string().data());
int fd{socket(PF_UNIX, SOCK_STREAM, 0)};
scoped_cleanup close_socket_at_exit{&fd, [](const int* f)
{
std::cout << "cleanup: close socket #" << *f << '\n';
close(*f);
}};
bind(fd, reinterpret_cast<sockaddr*>(std::addressof(addr)), sizeof addr);
fs::create_symlink("file", sandbox/"symlink");
for (std::filesystem::directory_entry entry: fs::directory_iterator(sandbox))
print_entry_type(entry);
// Request file system objects status directly:
for (const char* str : {"/dev/null", "/dev/cpu", "/usr/include/c++",
"/usr/include/asm", "/usr/include/time.h"})
print_entry_type(fs::directory_entry{str});
} // Cleanup via scoped_cleanup objects
π Possible output π
"sandbox/symlink": is a regular file (a symlink)
"sandbox/sock": is a named IPC socket (an `other` file)
"sandbox/pipe": is a named IPC pipe (an `other` file)
"sandbox/dir": is a directory
"sandbox/file": is a regular file
"/dev/null": is a character device (an `other` file)
"/dev/cpu": does not exist
"/usr/include/c++": is a directory
"/usr/include/asm": is a directory (a symlink)
"/usr/include/time.h": is a regular file
cleanup: close socket #3
cleanup: remove_all("sandbox")
look at that output slay, it's giving comprehensive filesystem tour π₯
π§ SEE ALSO π§
is_otherβ checks whether the argument refers to an other file (C++17) (function). iconic of her to reference herself, honestly. π
that's the whole page bestie, iykyk π β source is http://cppreference.com, dated 2024.06.10, and this is page std::filesy...y::is_other(3). go forth and vibe check your filesystem π¦β¨
π¦ 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