2

我有以下示例代码,我在 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但并不完全理解

        _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);
    }

有没有人可以更详细地解释我以了解问题?

4

1 回答 1

2

Compiler Explorer 在POSIX系统上运行其大部分编译器。POSIX允许实现将前导解释为//foo类似于 Windows 如何将前导\\foo: 解释为网络文件系统的主机名。/net(Windows实际上支持任何一种斜线,如您的示例中所示。 )但是,现代实现不这样做(而是依赖于在某个目录中自动挂载//foo/foo例如/. filesystem::path不会将该全局根视为具有Windows意义上的名称\\hostC:来自 Windows 的名称。

于 2020-07-05T15:51:34.437 回答