std::filesystem::relative() works fine on Linux and macOS, but it doesn't work as expected on Windows. Except for the current path, the other path is ok.
Anyone knows why?
Sample code
#include <iostream>
#include <filesystem>
using std::cout;
using std::endl;
namespace fs = std::filesystem;
int main()
{
auto f = fs::path(".\\CMakeLists.txt");
cout << f.make_preferred() << endl;
cout << relative(f) << endl;
cout << "======================================" << endl;
auto h = fs::path("./CMakeLists.txt");
cout << h.make_preferred() << endl;
cout << relative(h) << endl;
cout << "======================================" << endl;
auto g = fs::path("..\\CMakeLists.txt");
cout << g.make_preferred() << endl;
cout << relative(g) << endl;
cout << "======================================" << endl;
auto k = fs::path("../CMakeLists.txt");
cout << k.make_preferred() << endl;
cout << relative(k) << endl;
return 0;
}
Windows Result:
".\\CMakeLists.txt"
""
======================================
".\\CMakeLists.txt"
""
======================================
"..\\CMakeLists.txt"
"..\\CMakeLists.txt"
======================================
"..\\CMakeLists.txt"
"..\\CMakeLists.txt"
macOS 12.6 M1 Result
".\\CMakeLists.txt"
".\\CMakeLists.txt"
======================================
"./CMakeLists.txt"
"CMakeLists.txt"
======================================
"..\\CMakeLists.txt"
"..\\CMakeLists.txt"
======================================
"../CMakeLists.txt"
"../CMakeLists.txt"