A tiny C++23 filesystem wrapper that returns std::expected instead of throwing
Hi everyone,
I’ve been working on a small C++23 header-only library called **expected_fs**:
https://github.com/MrZLeo/expected-fs
It’s a thin wrapper around `std::filesystem` where fallible operations return `std::expected<T, std::error_code>` instead of throwing `std::filesystem_error`.
The idea is not to replace `std::filesystem`, but to make filesystem code easier to compose in projects that prefer explicit error handling.
Example:
```cpp
#include <expected_fs/expected_fs.hpp>
#include <iostream>
int main() {
const auto size = expected_fs::file_size("data.txt");
if (!size) {
std::cerr << size.error().message() << '\n';
return 1;
}
std::cout << *size << '\n';
}
```
Most function names mirror `std::filesystem`, so usage should feel familiar:
```cpp
auto created = expected_fs::create_directories("out/cache");
auto copied = expected_fs::copy_file("input.txt", "out/input.txt");
auto removed = expected_fs::remove("out/input.txt");
```
It's simple and it just work. I’d love feedback on the API shape, naming, CMake packaging, and whether this feels useful to people who avoid exception-based filesystem handling.
Thanks for taking a look! :)
https://redd.it/1ubq20l
@r_cpp
Post #25465
16