Praveer Kumar
Praveer Kumar

Reputation: 1018

why does std::filesystem::path::root_name() return empty string?

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 ?

Upvotes: 1

Views: 1110

Answers (1)

Davis Herring
Davis Herring

Reputation: 40013

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.

Upvotes: 2

Related Questions