std::remove 和 boost::filesystem::remove 的区别?

Difference between std::remove and boost::filesystem::remove?

在 C++17 文件系统库中,我们得到了 std::filesystem::remove(path),据我所知,它是 Boost.Filesystem.[=23= 的 boost::filesystem::remove(path) 的直接端口]

但 C++ 从 C89 继承了一个非常相似的函数,称为 std::remove(path) 将其记录为一种从文件系统中删除文件的方法。我隐约意识到这个功能的一些缺陷,例如我相信我听说 Windows std::remove 不能用于删除当前进程仍保持打开状态的文件。

std::filesystem::remove 是否用 std::remove 解决了这些问题?我应该更喜欢 std::filesystem::remove 而不是 std::remove?或者前者只是后者的命名空间同义词,具有相同的缺点和陷阱?

我的问题的标题询问 boost::filesystem::remove(path)std::remove(path) 之间的区别,因为我认为 std::filesystem::remove(path) 可能还没有被很多库供应商实现,但是我的理解是它应该基本上是 Boost 版本的直接副本。因此,如果您在 Windows 上了解 Boost.Filesystem,您可能也知道足以回答这个问题。

检查随我的 MSVC 安装的标准库源代码,std::experimental::filesystem::remove 调用其内部 _Unlink 帮助器,它只是调用 _wremove, which simply calls Windows DeleteFileW。同样,boost::filesystem::remove 也只是在 Windows 上调用 DeleteFileW


std::filesystem::remove is specified by reference to POSIX remove, but the global wording in [fs.conform.9945] 明确表示不需要实现来提供确切的 POSIX 行为:

Implementations should provide such behavior as it is defined by POSIX. Implementations shall document any behavior that differs from the behavior defined by POSIX. Implementations that do not support exact POSIX behavior should provide behavior as close to POSIX behavior as is reasonable given the limitations of actual operating systems and file systems. If an implementation cannot provide any reasonable behavior, the implementation shall report an error as specified in [fs.err.report]. [ Note: [...] ]

Implementations are not required to provide behavior that is not supported by a particular file system. [ Example: [...] ]

::remove 中的任何怪癖(即关于删除的实际行为而不是识别要删除的文件)可能是由于底层 OS API 的限制.我认为没有理由认为 std::filesystem::remove 在同一操作系统上的实现会神奇地做得更好。