Remove dependency on boost::filesystem::current_path()

390 Views Asked by At

I have a single line of code for which I include the boost-filesystem 1.64 library which I'd love to remove, so I can remove the dependency on Boost altogether from my program.

The line itself:

std::string currentPath = boost::filesystem::current_path().string();

I'm searching for a replacement that gives me a std::string currentPath that works on Windows and Linux with the compilers Visual C++ and LLVM. If possible also for GCC.

It's probably true that I haven't looked hard enough, but I am still learning C++ and don't actually know a lot about the standard library. Therefore I'm asking this question.

The current solution is to rely on:

std::experimental::filesystem::current_path();
1

There are 1 best solutions below

1
On BEST ANSWER

I suggest you take the source code from Boost, adapt/decrustify it and move on. It is not much more than a wrapper around getcwd() (POSIX) and GetCurrentDirectoryW (Windows). You can throw it out later when std::filesystem::current_path() becomes broadly available.

If you wonder how BOOST_POSIX_API is set (referenced in the method), have a look at this snippet:

# if defined(_WIN32) || defined(__CYGWIN__) // Windows default, including MinGW and Cygwin
#   define BOOST_WINDOWS_API
# else
#   define BOOST_POSIX_API 
# endif