【发布时间】:2018-08-16 22:22:03
【问题描述】:
从#include <experimental/filesystem> 升级到#include <filesystem> 时遇到问题。似乎std::filesystem::path::wstring 方法没有返回与experimental::filesystem 相同的字符串。我编写了以下包含输出结果的小测试程序。
#include <iostream>
#include <filesystem>
#include <experimental/filesystem>
namespace fs = std::filesystem;
namespace ex = std::experimental::filesystem;
using namespace std;
int main()
{
fs::path p1{ L"C:\\temp/foo" };
wcout << "std::filesystem Native: " << p1.wstring() << " Generic: " << p1.generic_wstring() << endl;
ex::path p2{ L"C:\\temp/foo" };
wcout << "std::experimental::filesystem Native: " << p2.wstring() << " Generic: " << p2.generic_wstring() << endl;
}
/* Output:
std::filesystem Native: C:\temp/foo Generic: C:/temp/foo
std::experimental::filesystem Native: C:\temp\foo Generic: C:/temp/foo
*/
据https://en.cppreference.com/w/cpp/filesystem/path/string:
返回值
本地路径名格式的内部路径名, 转换为指定的字符串类型。
该程序在 Windows 10 上运行,并使用 Visual Studio 2017 版本 15.8.0 编译。我希望本机路径名是C:\temp\foo。
问题:这是std::filesystem::path 中的错误吗?
【问题讨论】:
-
Windows 上的 C 运行时 API 是“易变的”,因为它们接受 ` \ ` 或 ` / ` 作为分隔符。 Win32 API 不太宽容,特别是如果您使用 Long UNC,并且需要使用 ` \ `。
-
在 Visual Studio 中,
<filesystem>是 Microsoft 特定的,如 here 所述。 -
std::experimental的部分观点是,您应该期望最终版本的行为有所不同,并愿意接受最终的差异。 -
无论这是否是“错误”,您都可以调用
path::make_preferred()将任何前斜杠转换为反斜杠。 -
这不是标准中的错误——问题是它是否是 MSVC 实现中的错误
标签: c++ c++17 std-filesystem