为何path::root_name()在<filesystem>与<experimental/filesystem>中行为不同?
Hey there! I’ve been digging into the behavior of path handling functions in both std::experimental::filesystem and the finalized std::filesystem libraries recently, and ran some tests on Godbolt that showed interesting differences. Let me share what I found, plus how you can look into the actual implementations yourself.
Test Results: Network Path //net
1. std::experimental::filesystem
Code
#include <iostream> #include <experimental/filesystem> namespace fs = std::experimental::filesystem; int main() { auto p = fs::path("//net"); std::cout<<"p = " << p <<"\np.root_name= "<< p.root_name() <<"\nand p.root_Dir= "<< p.root_directory() <<"\np.is_absolute= "<<p.is_absolute()<<std::endl; }
Output
p = "//net" p.root_name= "//net" and p.root_Dir= "" p.is_absolute= 0
2. std::filesystem
Code
#include <iostream> #include <filesystem> namespace fs = std::filesystem; int main() { auto p = fs::path("//net"); std::cout<<"p = " << p <<"\np.root_name= "<< p.root_name() <<"\nand p.root_Dir= "<< p.root_directory() <<"\np.is_absolute= "<<p.is_absolute()<<std::endl; }
Output
p = "//net" p.root_name= "" and p.root_Dir= "/" p.is_absolute= 1
How to View the Implementations
Absolutely! There are a few straightforward ways to access the source code for these library functions:
Check your local standard library headers: Most compilers install the source or annotated header files alongside their binary libraries. For example:
- If you’re using GCC’s
libstdc++, look in directories like/usr/include/c++/<your-gcc-version>/experimental/filesystem(for the experimental variant) and/usr/include/c++/<your-gcc-version>/filesystem(for the standard one). Implementation details are often in inline functions within the headers or linked.hfiles likepath.h. - For Clang’s
libc++, the headers live in paths like/usr/include/c++/v1/experimental/filesystemand/usr/include/c++/v1/filesystem.
- If you’re using GCC’s
Browse open-source repos: Both major standard library implementations are open source:
- GCC’s
libstdc++code lives in the GCC project’s repository, under thelibstdc++-v3directory. - Clang’s
libc++is part of the LLVM project, in thelibcxxdirectory.
- GCC’s
Use Godbolt’s tools: On Godbolt, you can switch the output view to "Source" (when available) to peek at the underlying header code, or generate assembly and trace back to how the functions are implemented under the hood.
内容的提问来源于stack exchange,提问作者Praveer Kumar

