为什么不按默认构造路径划分路径只是在 Visual Studio 中添加尾随分隔符?

Why Doesn't Dividing a path by the Default Constructed path Just add a Trailing Separator in Visual Studio?

本文关键字:路径 添加 Studio 分隔符 Visual 默认 划分 为什么不      更新时间:2023-10-16

假设filesystem::current_path将返回路径:

/

tmp/1567519419.773012

但我想要一个尾随分隔符。如果看起来我应该做的是:

filesystem::current_path() / filesystem::path()

这适用于 gcc 给我:

/

tmp/1567519419.773012/

但是在可视化工作室上,虽然filesystem::current_path也为我提供了没有尾随分隔符的绝对路径,但除以filesystem::path()没有任何效果。生成的路径仍然是没有尾随分隔符绝对路径。

我想要跨平台兼容的代码,并且我想避免检测当前路径是否已经具有尾随分隔符。

有什么可供我使用的吗?

我对filesystem不够熟悉,无法知道哪个编译器是正确的(如果涉及实现定义的行为,可能两者都是正确的(。但是,以下内容应该适用于正确实现filesystem的所有平台:

#include <iostream>
#include <filesystem>
int main() {
auto foo = std::filesystem::current_path();
foo += foo.preferred_separator; // A static data member of std::filesystem::path
// The lexically normal form eliminates doubled separators
std::cout << foo.lexically_normal().string() << 'n';
}