I am trying to port from boost::filesystem to std::filesystem. During the process I encountered some code in which boost and std seems to behave in a different way.
The following code shows the behaviour:
#include <iostream> #include <filesystem> #include <boost/filesystem.hpp> template<typename T> void TestOperatorSlash() { const std::string foo = "Foo"; const std::string bar = "Bar"; const T start = "\\"; std::string sep; sep += T::preferred_separator; const auto output = start / (foo + bar) / sep; std::cout << output << '\n'; } int main(int argc, char** argv) { TestOperatorSlash<std::filesystem::path>(); TestOperatorSlash<boost::filesystem::path>(); } The code gives in output:
"\\" "\FooBar\" The expected behaviour for me is the one of boost, and I don't understand what happens with std::filesystem.
Why do I get "\\"? Why is the behaviour of operator/ of path different in boost and std?
Edit:
After understanding the motivation of the behaviour of std::filesystem::operator/ and given the answer to this question, I decided to use the function path::relative_path() for the second argument of operator/ when I have an absolute path.
In this way I can mimic the behaviour of boost::filesystem. So, for example:
#include <iostream> #include <filesystem> namespace fs = std::filesystem; int main(int argc, char** argv) { fs::path foo = "\\foo"; fs::path bar = "\\bar"; auto foobar0 = foo / bar; // Evaluates to \\bar auto foobar1 = foo / bar.relative_path(); // Evaluates to \\foo\\bar }
/in apath, you can use/ "", which produces a result that is of course one character shorter than using/ "a".