C++ 为什么std::filesystem::path::root\u name()返回空字符串?
下面是我在VisualStudio2019和上运行的示例代码。我可以看出有两种不同的行为。可能的原因是什么C++ 为什么std::filesystem::path::root\u name()返回空字符串?,c++,windows,c++17,std-filesystem,C++,Windows,C++17,Std Filesystem,下面是我在VisualStudio2019和上运行的示例代码。我可以看出有两种不同的行为。可能的原因是什么 #include <iostream> #include <filesystem> int main() { std::filesystem::path p("//net"); std::cout << "root name of " << p << &q
#include <iostream>
#include <filesystem>
int main()
{
std::filesystem::path p("//net");
std::cout << "root name of " << p << " is " << p.root_name() << std::endl;
}
有没有人能更详细地向我解释这个问题?编译器资源管理器在POSIX系统上运行大多数编译器。POSIX允许实现将前导的
//foo
解释为网络文件系统的主机名,类似于Windows将前导的\\foo
:解释为网络文件系统的主机名。(如您的示例所示,Windows实际上支持任意一种斜杠。)但是,现代实现不支持这一点(而是依赖于在某些目录中自动装载,如/net
),因此/foo
只是指/foo
,与其他所有内容一样,它位于一个Unix根目录下//code><代码>文件系统::path
不将全局根目录计算为具有Windows中的\\host
或C:
意义上的名称。可能是UNIX/Windows文件命名问题?Linux技术上不包括根目录名称<如果std::filesystem在任何方面与boost::filesystem内部类似,则code>root\u path返回正确的路径(/
):“因为没有根名称(即驱动器说明符或网络名称),所以单斜杠(或反斜杠)在Windows上是相对路径,但在类似POSIX的操作系统上是绝对路径。”root\u name()
可能适用于实际具有根名称的Windows和其他操作系统
_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);
}