C++ Logo


Advanced search

fs::path appenders have inconsistent behavior for UNC paths between OSes

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Fri, 5 Nov 2021 01:41:17 +0300

Consider the following test case:

  #include <iostream>
  #include <filesystem>

  int main()
    std::cout << (std::filesystem::path("//net/foo") / "/bar")
      << std::endl;

Assuming "//net" is recognized as a root name, this test would output
"/bar" on POSIX systems and "//net/bar" on Windows. This is because
"/bar" is absolute on POSIX but not on Windows (where a root name such
as "C:" or "\\net" is required for absolute paths), and
[fs.path.append]/2 says:

  path& operator/=(const path& p);

  If p.is_absolute() || (p.has_root_name() && p.root_name() !=
  root_name()), then operator=(p).

Besides the difference in behavior, the result of "/bar" seems
unexpected, as appending a path starting with a root directory is
expected to be rebased on top of the source's root name.

My question is, is this behavior intentional or is this a defect? If it
is intentional, what is the rationale?

Received on 2021-11-04 17:41:22