2

I have below sample code which I ran on visual studio 2019 and on https://godbolt.org/. I can see that there are two different behavior. What could be the possible reason ?

#include <iostream>
#include <filesystem>


int main()
{    
    std::filesystem::path p("//net");    
    std::cout << "root name of " << p << " is " << p.root_name() << std::endl;
}

Output in visualstudio 2019 : root name of "//net" is "//net"

Output in https://godbolt.org/ : root name of "//net" is "" enter image description here

I Just read http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4100.pdf - 8.4.9 path decomposition [path.decompose] but did not understand the reason.

Also, I read below code in the std::filesystem but din not understand completely

        _NODISCARD path root_name() const {
        // parse the root-name from *this and return a copy if present; otherwise, return the empty path
        return _Parse_root_name(_Text);
    }

Is there anyone who can explain me in more detail to understand the problem ?

Davis Herring
  • 36,443
  • 4
  • 48
  • 76
Praveer Kumar
  • 912
  • 1
  • 12
  • 25
  • 1
    Maybe it is a UNIX / Windows file naming issue? – PaulMcKenzie Jul 05 '20 at 09:37
  • Linux technically doesn't include a root name. `root_path` returns the right path though (`/`) – Zoe Jul 05 '20 at 12:03
  • 1
    If std::filesystem is in any way similar to boost::filesystem internally, [this might be relevant](https://www.boost.org/doc/libs/1_68_0/libs/filesystem/doc/tutorial.html): "Because there is no root name (i.e. drive specifier or network name), a lone slash (or backslash) is a relative path on Windows but an absolute path on POSIX-like operating systems.". `root_name()` is probably meant for Windows and other operating systems that actually have root names – Zoe Jul 05 '20 at 12:05

1 Answers1

2

Compiler Explorer runs most of its compilers on a POSIX system. POSIX allows an implementation to interpret a leading //foo similarly to how Windows interprets a leading \\foo: as a host name for a network filesystem. (Windows actually supports either kind of slash, as seen in your example.) However, modern implementations do not do this (relying instead on automounting in some directory like /net), so //foo just means /foo, which like everything else is under the one Unix root directory /. filesystem::path doesn’t count that global root as having a name in the sense of \\host or C: from Windows.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76