I see different outputs when running this piece of code in gcc7.3 (using C++14) and gcc9.3 (using C++17):
#include <iostream>
#if (__cplusplus >= 201703L)
#include <filesystem>
namespace fs = std::filesystem;
#else
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif
using namespace std;
std::string getBaseName(const std::string& filePath) {
return fs::path(filePath).filename().string();
}
int main()
{
std::cout<<"getBaseName(/test/absolute/dir/)="<<getBaseName("/test/absolute/dir/")<<std::endl;
std::cout<<"getBaseName(/)="<<getBaseName("/")<<std::endl;
return 0;
}
In gcc7.3 (C++14), it gave me:
getBaseName(/test/absolute/dir/)=.
getBaseName(/)=/
In gcc9.3(C++17), I got:
getBaseName(/test/absolute/dir/)=
getBaseName(/)=
I wonder if this is a bug in gcc7.3 (and therefore std::experimental) and if so, do we have any workaround without relying on any third party libraries?