为什么不按默认构建的路径划分路径只需在 Visual Studio 中添加一个尾随分隔符?
Why Doesn't Dividing a path by the Default Constructed path Just add a Trailing Separator in Visual Studio?
假设 filesystem::current_path
将 return 路径:
/tmp/1567519419.773012
但我想要一个尾随分隔符。如果看起来我应该做的就是:
filesystem::current_path() / filesystem::path()
/tmp/1567519419.773012/
但是在 visual-studio 上,虽然 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';
}
假设 filesystem::current_path
将 return 路径:
/tmp/1567519419.773012
但我想要一个尾随分隔符。如果看起来我应该做的就是:
filesystem::current_path() / filesystem::path()
/tmp/1567519419.773012/
但是在 visual-studio 上,虽然 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';
}