4

I have the following code snipped, which produces the following output on mingw(gcc) and msvc:

std::cout << std::filesystem::relative("D:/home/freddy", "C:/home") << std::endl;

mingw(gcc):

"\\home\\freddy"

msvc:

""

gcc and also clang (both executed on linux):

"../../D:/home/freddy"

In my opinion it should be "" or "../../D:/home/freddy", since there is no relative path over root_paths, but not the relative path of an erased root path.

Additionally MSVC will fail, when the drive does not exist, because the drive is "not ready yet": weakly_canonical: Das Gerõt ist nicht bereit.: "D:/home/freddy" Which also seems like a MSVC bug in std::filesystem::weakly_canonical

Fabian Keßler
  • 563
  • 3
  • 12
  • 1
    Note: I find that [`std::filesystem::path::lexically_relative`](https://en.cppreference.com/w/cpp/filesystem/path/lexically_normal) is generally what we want it those case (no check of symlink, existance of path, ... (`D:\` currently might be alias/virtual drive to `C:\`)). – Jarod42 Sep 02 '20 at 17:32
  • 1
    @Jarod42 Regardless of what you prefer, OP is still seeing inconsistent results with `std::filesystem::relative`. – Jesper Juhl Sep 02 '20 at 17:42
  • No I want std::filesystem::relative, since it can be resolve symlinks if there are some. But normally std::filesystem::weakly_canonical should not fail in such a case: "Returns a path composed by operator/= from the result of calling canonical() with a path argument composed of the leading elements of p that exist (as determined by status(p) or status(p, ec)), if any, followed by the elements of p that do not exist, if any." Since D:/ is a Path which does not exist, it should be just appended. But as @JesperJuhl said this is mostly for the std::filesystem::relative inconsistency. – Fabian Keßler Sep 02 '20 at 18:41
  • I'm not sure what you would expect to happen, it's not clear from your question. In any case, this might be a bug (an unsupported corner case) in the standard library implementation. – Ulrich Eckhardt Sep 02 '20 at 18:47
  • 1
    1) On Windows, `"C:/home"` is incorrect. It should be `"C:\\home"`. 2) `"../../D:/home/freddy"` is an impossible path, as drive letter must be at the beginning. 3) A cross-disk relative path does not exist. So `""` is the only possibility. – rustyx Sep 02 '20 at 20:02
  • Ok I think this is a layer 8 problem or it got fixed with a mingw update. But the mingw and the MSVC implementation still differ when one of the Driveletters is not used. – Fabian Keßler Sep 03 '20 at 21:54

0 Answers0