In the C++17 filesystem library, we got std::filesystem::remove(path)
, which — as I understand it — is a direct port of boost::filesystem::remove(path)
from Boost.Filesystem.
But C++ inherited from C89 a very similar function called std::remove(path)
, which is also documented as a way to remove a file from the filesystem. I'm vaguely aware of some pitfalls with this function, e.g. I believe I have heard that on Windows std::remove
cannot be used to remove a file that is still being held open by the current process.
Does std::filesystem::remove
fix these issues with std::remove
? Should I prefer std::filesystem::remove
over std::remove
? Or is the former just a namespaced synonym for the latter, with the same warts and pitfalls?
The title of my question asks for the difference between boost::filesystem::remove(path)
and std::remove(path)
because I figure that std::filesystem::remove(path)
may not have been implemented by a lot of library vendors yet, but my understanding is that it's supposed to be basically a direct copy of the Boost version. So if you know about Boost.Filesystem on Windows, you probably know enough to answer this question too.
Checking the standard library sources installed with my MSVC, std::experimental::filesystem::remove
calls its internal _Unlink
helper, which simply calls _wremove
, which simply calls Windows DeleteFileW
. Similarly, boost::filesystem::remove
also just calls DeleteFileW
on Windows.
std::filesystem::remove
is specified by reference to POSIX remove
, but the global wording in [fs.conform.9945] makes clear that implementations are not required to provide the exact POSIX behavior:
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: [...] ]
Any quirks in ::remove
(that is about the actual act of removing rather than identification of the file to be removed) are likely due to limitations of the underlying OS API. I see no reason to think that an implementation of std::filesystem::remove
on the same operating system will magically do better.