简体   繁体   中英

Using std::experimental::filesystem with g++ 9.3.0

I'm using VS2015 as my main developing tool and to use std::filesystem there I need to write something like this:

namespace fs = std::experimental::filesystem;

Now I need to compile this code using g++ 9.3.0 and, of course, get an error about "experimental" part. Is there a way to keep this oldy thing in my code and compile it with modern g++?

Use namespace fs = std::filesystem; for g++. If you need to compile the same code in both compilers, use #if to differentiate between them.

I would use the version of C++ standard in combination with a pre-processor directive to distinguish between them as it the filesystem library has been officially introduced in C++17

#if __cplusplus >= 201703L
  #include <filesystem>
  namespace fs = std::filesystem;
#else
  #include <experimental/filesystem>
  namespace fs = std::experimental::filesystem;
#endif

In case this occurs to you with another library in C++17 you can use __has_include

#if __has_include(<some_lib>)
  #include <some_lib>
  namespace sl = std::some_lib;
#else
  #include <experimental/some_lib>
  namespace sl = std::experimental::some_lib;
#endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM