如果p以根路径开头,为什么std :: filesystem :: path :: append替换当前路径 [英] Why does std::filesystem::path::append replace the current path if p starts with root path

查看:118
本文介绍了如果p以根路径开头,为什么std :: filesystem :: path :: append替换当前路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下代码:

  #include< iostream> 
#include< filesystem>

命名空间fs = std :: filesystem;

int main()
{
fs :: path fsBase = / base;
fs :: path fsAppend = / append;
自动fsResult = fsBase / fsAppend;

std :: cout<< fsResult:<< fsResult<< std :: endl;
返回0;
}

通常,预期结果为 / base / append ,但实际上给出了 / append



fs :: path :: append 确实表明了这种行为:


如果p.is_absolute()|| (p.has_root_name()&& p.root_name()!= root_name()),然后将当前路径替换为p,就像操作符=(p)一样,完成操作。


但是, std :: experimental :: filesystem boost :: filesystem 不同,它给出了预期的 / base / append
请参见示例



问题是为什么它会这样表现?为什么用 append()函数替换路径?

解决方案

fsAppend 是绝对路径,因为它以 / 开头,并且您使用的是POSIX这样的系统 / 是绝对的。



将一个绝对路径附加到另一个绝对路径没有任何意义(对我来说实际上是最自然的结果)。 C:\foo.txt 附加 C:\bar.txt 的结果应该是什么?

experimental :: fs 中,规则是如果第二个参数为 .native()从目录分隔符开始,然后将其视为用于追加目的的相对路径,即使在其他情况下它可能是绝对路径!



标准化文件系统清楚地将绝对路径与相对路径区分开来,试图避免在POSIX系统上出现这种歧义。



可以在 P0492R2 US77



请注意,您可以使用 + = 而不是 / 进行串联(应该做您期望的事情),或者在使用 / 之前将第二个参数设为相对值。



另请参见此答案以进一步比较实验并完成。


Given below code:

#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

int main()
{
    fs::path fsBase = "/base";
    fs::path fsAppend = "/append";
    auto fsResult = fsBase / fsAppend;

    std::cout << "fsResult: " << fsResult << std::endl;
    return 0;
}

Usually, the expected result is /base/append, but it actually gives /append.

The description of fs::path::append does indicate this behavior:

If p.is_absolute() || (p.has_root_name() && p.root_name() != root_name()), then replaces the current path with p as if by operator=(p) and finishes.

However, the behavior of std::experimental::filesystem and boost::filesystem is different, that gives expected /base/append. See examples.

The question is why it behaves like this? Why does it replace the path with append() function?

解决方案

fsAppend is an absolute path since it starts with / and you're on a system such as POSIX where paths starting with / are absolute.

Appending one absolute path to another absolute path doesn't make any sense (to me throwing an exception would be the most natural result actually). What should the result of C:\foo.txt append C:\bar.txt be?

In experimental::fs the rule was that if the second argument's .native() started with a directory separator then it was treated as a relative path for append purposes, even though it may be an absolute path in other contexts!

The standardized filesystem clearly distinguishes absolute paths from relative paths, trying to avoid this ambiguity that arises on POSIX systems.

The write-up of the change can be found in P0492R2 US77.

Note that you can use += rather than / for concatenation (should do what you expect), or make the second argument relative before using /.

Also see this answer for further comparison between experimental and finalized.

这篇关于如果p以根路径开头,为什么std :: filesystem :: path :: append替换当前路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆