我下面有在Visual Studio 2019和https://godbolt.org/上运行的示例代码。我可以看到有两种不同的行为。可能是什么原因?
#include <iostream>
#include <filesystem>
int main()
{
std::filesystem::path p("//net");
std::cout << "root name of " << p << " is " << p.root_name() << std::endl;
}
visualstudio 2019中的输出:“//net”的根名称是“//net”https://godbolt.org/中的输出:“//net”的根名称为“”
我刚读http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4100.pdf-8.4.9路径分解[path.decompose]
但不明白原因。
另外,我阅读了
std::filesystem
中的以下代码,但din并不完全理解 _NODISCARD path root_name() const {
// parse the root-name from *this and return a copy if present; otherwise, return the empty path
return _Parse_root_name(_Text);
}
有谁可以更详细地解释我的理解问题? 最佳答案
编译器资源管理器在 POSIX 系统上运行大多数编译器。 POSIX允许实现类似于Windows将前导//foo
解释为网络文件系统的主机名的方式来解释前导\\foo
。 (如您的示例所示,Windows实际上支持两种斜杠。)但是,现代实现不执行此操作(而是依靠自动挂载到/net
这样的目录中),因此//foo
只是意味着/foo
,就像其他所有内容都在该斜杠下一样。 Unix根目录/
。从Windows的filesystem::path
或\\host
的角度来看,C:
不会将该全局根视为具有名称。
关于c++ - 为什么std::filesystem::path::root_name()返回空字符串?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62739003/