为什么目录上的std :: filesystem :: file_size留给实现?

因此,我遇到了以下问题:我需要总结特定目录中所有文件的字节大小,其中包括子目录的大小,因为在我看来,它们实际上可以增加大小。

但是,如果我们在包含文件和子目录的目录上运行此代码,则如下所示:

#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.

因此,任何人都可以启发我为什么将其留给实现,我可以使用C ++标准并且找不到很好的理由。

评论