So I have the following problem: I need to summarise the byte size of all files in a specific directory, this includes the size of the sub-directories, as in my case they actually can increase in size.
But if we run this code on a directory that contains files and sub-directories and look like this:
#include <filesystem>
#include <iostream>
#include <cstdint>
int main(void)
{
std::uintmax_t result = 0;
for (const auto& path : std::filesystem::directory_iterator("."))
{
result += std::filesystem::file_size(path)
}
std::cout << "Total size is: " << result << std::endl;
return 0;
}
Then you will get an error that you are are trying to get the file size of a directory. If you run it on macOS or Linux at least compiling with Clang++ 10 or 11. Now according to Cppreference on std::filesystem::file_size getting the size of directory is up to the implementation. However, in my opinion, this is weird as file_size basically just "wraps" stat and therefore should work perfectly on a directory, at least on Linux, *BSD, and macOS.
So can anyone enlightment me as to why this has been left to the implementation, I have access to the C++ standard and cannot find a good reason.