Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

c++ - Why does std::filesystem::path::append replace the current path if p starts with root path

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:ar.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...