How to determine whether to use <filesystem> or <experimental/filesystem>? - c++

Is there a way to determine whether I can use the standard <filesystem> (which is available on all modern C++ compilers that support C++17) or <experimental/filesystem> which is used by older compilers. (For example g++ 6.3, which is the current standard version on Debian Stretch)
Knowing which one to use is imporant because the first uses std::filesystem::xxx and the latter std::experimental::filesystem::xxx.

I typically create a header filesystem.hpp with the following content:
// We haven't checked which filesystem to include yet
#ifndef INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
// Check for feature test macro for <filesystem>
# if defined(__cpp_lib_filesystem)
# define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 0
// Check for feature test macro for <experimental/filesystem>
# elif defined(__cpp_lib_experimental_filesystem)
# define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1
// We can't check if headers exist...
// Let's assume experimental to be safe
# elif !defined(__has_include)
# define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1
// Check if the header "<filesystem>" exists
# elif __has_include(<filesystem>)
// If we're compiling on Visual Studio and are not compiling with C++17, we need to use experimental
# ifdef _MSC_VER
// Check and include header that defines "_HAS_CXX17"
# if __has_include(<yvals_core.h>)
# include <yvals_core.h>
// Check for enabled C++17 support
# if defined(_HAS_CXX17) && _HAS_CXX17
// We're using C++17, so let's use the normal version
# define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 0
# endif
# endif
// If the marco isn't defined yet, that means any of the other VS specific checks failed, so we need to use experimental
# ifndef INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
# define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1
# endif
// Not on Visual Studio. Let's use the normal version
# else // #ifdef _MSC_VER
# define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 0
# endif
// Check if the header "<filesystem>" exists
# elif __has_include(<experimental/filesystem>)
# define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1
// Fail if neither header is available with a nice error message
# else
# error Could not find system header "<filesystem>" or "<experimental/filesystem>"
# endif
// We priously determined that we need the exprimental version
# if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
// Include it
# include <experimental/filesystem>
// We need the alias from std::experimental::filesystem to std::filesystem
namespace std {
namespace filesystem = experimental::filesystem;
}
// We have a decent compiler and can use the normal version
# else
// Include it
# include <filesystem>
# endif
#endif // #ifndef INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
It even creates an alias for std::experimental::filesystem to std::filesystem if the experimental headers are used.
Which means you can simply include this header in place of <filesystem>, use std::filesystem::xxx and enjoy support from older compilers too.
A few notes on the details of this snippet:
__cpp_lib_filesystem and __cpp_lib_experimental_filesystem
These are Feature Testing Macros. They should be available when the respecitive headers are available. But VisualStudio 2015 (and below) don't support them. So the rest is just to make sure we can make an accurate assesment, instead of relying on unreliable macros.
__has_include()
While most compilers do have that macro built in, there is no gurantee, as it is not in the standard. My snippet checks for it's existence before it is used. And in case it doesn't exist, we assume we have to use the experimental version to provide maximum compatibility.
defined(_MSC_VER) && !(defined(_HAS_CXX17) && _HAS_CXX17)
Some versions of VisualStudio (namely 2015) have just an half arsed implementation of C++17. And it's possible that the <filesystem> header exists, but std::filesystem doesn't. This line checks for that case and uses the experimental version instead.
#error ...
If the header check is available and we can't find either header we just print a nice error, as there's nothing we can do.
INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
You even get a marco that let's you know which version is in usage so you could write pre processor statements of your own that deal with the differences between the versions.
namespace filesystem = experimental::filesystem;
This alias definition is just for convinice that'll make sure that you'll have std::filesystem, assuming your compiler let's you do it (I haven't seen a single one that doesn't allow that).
According to the standard defining anything in the std namespace is undefined behavior. So if your compiler, concience, colleguages, code standard or whatever complains, just define namespace fs = std::experimental::filesystem; in the upper block and namespace fs = std::filesystem; in the lower. (Just to be sure, if you do that, remove the namespace std { stuff)
P.S.: I created the answer and this question, because I spent an awful lot of time getting frustrated with older compilers not having the <filesystem> header. After a fair amount of research and testing on multiple platforms with multiple compilers and versions of them, I managed to come up with this universal solution. I have tested it with VisualStudio, g++ and clang (Only with versions that actually do have at least experimental support for C++17).
Should there be an issue with another compiler, let me know and I'll make it work for it too.

I typically use the feature test macros a lot for this type of problem. I am currently faced with this exact problem but I have used __cpp_lib_filesystem along with the using keyword.
// since C++ 20
#include <version>
#ifdef __cpp_lib_filesystem
#include <filesystem>
using fs = std::filesystem;
#elif __cpp_lib_experimental_filesystem
#include <experimental/filesystem>
using fs = std::experimental::filesystem;
#else
#error "no filesystem support ='("
#endif
I am using this on gcc-6 and up as well as clang-6, sadly no older copy of studio to test against but it works on 15.7 and up.

Related

Is it safe and legitimate to use std::experimental features in a library released to the public?

Suppose I'm writing a library which targets C++14-capable compilers.
In C++14, several standard library facilities were introduced as experimental, e.g. optional and filesystem.
Is it:
safe (for downstream developers)
legitimate
for me to utilize these features in my library even if only C++14 is supported? e.g. in the form
#if __cplusplus >= 201703L
#include <optional>
namespace mylib {
using std::optional;
using std::nullopt;
}
#else
static_assert(__cplusplus >= 201402L, "C++2014 is required to compile this program");
#include <experimental/optional>
namespace mylib {
using std::experimental::optional;
using std::experimental::nullopt;
}
#endif
Note: Of course, I mean in the case of downstream developers actually having to use these features themselves, not just for the case when I use them internally only with no outside exposure.

C++ an VS error: <experimental/filesystem> header providing std::experimental::filesystem is deprecated by Microsoft and will be REMOVED

I coded in C++ on Visual Studio (Windows 10) and got this error:
#error The <experimental/filesystem> header providing std::experimental::filesystem is deprecated by Microsoft \
and will be REMOVED. It is superseded by the C++17 <filesystem> header providing std::filesystem. \
You can define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING to acknowledge that you have received this warning.
With this headers:
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <filesystem>//If I will disable it nothing happens.
#include <experimental/filesystem> //If I will disable it happens another error.
namespace fs = std::experimental::filesystem;
using namespace std;
I've tried: #define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING in the main cpp file. It didnt help.
So then I paste this code from here:
#ifdef __cpp_lib_filesystem
#include <filesystem>
using fs = std::filesystem;
#elif __cpp_lib_experimental_filesystem
#include <experimental/filesystem>
using fs = std::experimental::filesystem;
#else
#error "no filesystem support ='("
#endif
Didn't helped too.
What is the easiest way to get out that error?
Add _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING to Preprocessor definitions.
Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions.
This solved the issue for me.
According to the link
C++17’s is supported. This is a completely new
implementation, incompatible with the previous std::experimental
version, necessitated by symlink support, bug fixes, and changes in
standard-required behavior. Currently, including provides
the new std::filesystem and the previous
std::experimental::filesystem, and including
provides only the old experimental implementation. The experimental
implementation will be REMOVED in the next ABI-breaking release of the
libraries.
MS abandoned 'experimental' in filesystem.
You could try to use #include <filesystem>instead of #include <experimental/filesystem> and the std::experimental::filesystem is slated for removal, this means you should use std::filesystem.
I had the same problem and then, when I removed the experimental filesystem and added:
#include <filesystem>
namespace fs = std::filesystem;
I would get the error:
namespace “std” has no member “filesystem”
The solution was to go to project properties and do as instructed in the link.
VS2017: E0135 namespace "std" has no member "filesystem"
Adding the define where necessary worked for me:
#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING 1;
ie.
#ifdef __cpp_lib_filesystem
#include <filesystem>
namespace fs = std::filesystem;
#elif __cpp_lib_experimental_filesystem
#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING 1;
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#error "no filesystem support ='("
#endif
I've been researching this same problem for weeks, and I'm using visual studio 2019 on an Unreal Engine 4 project. It's difficult to enable c++17 headers in visual studio and make them work. A lot of people online suggests using the boost library instead, but this is my attempt in using filesystem because I don't want to deal with trying to download / import external libraries, and so here's what I know...
** Before you start coding in visual studio, you should build / compile your project and see if Visual Studios needs anything set up; for me, I had to download / setup a free license with Incredibuild (there are tutorials online if someone needs to get this setup). Incredibuild is helpful and necessary to build projects in Visual Studio (at least if you're using Unreal Engine 4), but sometimes I get 1 or 2 "errors" in visual studio, but will compile in Unreal Engine 4 because those errors aren't actually errors (people say online that Incredibuild is weird, and wanted to mention that)**
1) How to enable c++17 Headers
When trying to #include which is a c++ version 17 header, it's necessary to enable c++17: Look inside the visual studio "solution explorer" to view the project files, and right-click the project itself and open "properties". Go to the "C/C++ language" tab and switch the version to c++17 (this is easy to find this online). Unless you're doing an Unreal Engine project because there is not a "C/C++ language" tab, so instead find the "NMake" tab and in the text-box to the right of "Addition Options" type in:
/std:c++17 OR /std:c++latest (Microsoft has a list I found online of others, but these are fine)
and visual studios will recognize as a header.
2) How NOT to get c++17 namespaces to work (things I've tried)
After the headers start working you can try to finally make a namespace variable:
namespace fs = std::filesystem; => ERROR: 'std does not have a namespace or class filesystem'
OR if you also #include you can try:
namespace fs = std::experimental::filesystem; OR namespace fs = std::experimental::filesystem::v1; => ERROR: 'we want you to define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING'
how does one fix these errors?
Online, I found that adding these lines of code to your ProjectName.Build.cs file (which can be found and edited after clicking on the file in "solution explorer") instead of the original declaration for PCHUsage:
PCHUsage = PCHUsageMode.NoSharedPCHs; PrivatePCHHeaderFile = "Folder.h"; CppStandard = CppStandardVersion.Cpp17;
this did get rid of the Errors when using std::filesystem BUT caused wayyyy more problems, and seemed harmful. After looking through other websites / possible solutions, it seems people online don't know how to actually use the filesystem library in visual studio, I've been looking for working code for weeks.
if you want to try and use the std::experimental::filesystem or std::experimental::filesystem::v1, I tried #define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING, but no dice.
I'm in the same boat as you man, and it looks like you didn't get much help from the previous comments. If we can't find a solution / video tutorial online, I have a plan B. Maybe we could try and write a c++17 project using some different software, or maybe we could write a program using a different software that could be launched instead of enabling c++17 into Visual Studio; for example, if you wanted to list the files in a directory, make an executable file in your preferred other software, and launch that executable / feed it arguments from your visual studio project. That's the best I got man. I hope that sparks some creativity or whatever, but I think it's a lot more helpful than what I've seen online. Good luck to you! and I need that luck too

How to selectively include boost headers depending on boost version

This change in Boost caused some header file locations to change: https://github.com/boostorg/property_tree/commit/ea940990691de91e9b22255d9b450fcdac237646
I am working on a codebase where some users build with an older version of boost, with the old header locations, e.g. #include <boost/property_tree/detail/json_parser_error.hpp> instead of the newer #include <boost/property_tree/json_parser/error.hpp>. I want to retain compatibility with both older (pre-1.61) and newer boosts.
Is there a way to instruct the compiler to check for the boost version and use the new header include if the version >= 1.61?
I think you are looking for BOOST_VERSION preprocessor. It is defined in boost\version.hpp
// Caution, this is the only boost header that is guarenteed
// to change with every boost release, including this header
// will cause a recompile every time a new boost version is
// released.
//
// BOOST_VERSION % 100 is the patch level
// BOOST_VERSION / 100 % 1000 is the minor version
// BOOST_VERSION / 100000 is the major version
Simple usage would be:
#if BOOST_VERSION >= 106100
#include <this.hpp>
#else
#include <that.hpp>
#endif

Use of #include <iostream.h>

I am working on an older project that still has the deprecated "#include iostream.h" inclusions. I understand that iostream.h is deprecated and should not be used, but some of the systems that this code has to run/compile on are old solaris machines running CC and do not have iostream available. My question is: how can I make my more modern g++ compiler accept the iostream.h inclusions.
EDIT: The compilier cannot find the iostream.h file so I am assuming that none of the .h versions of the library are available to g++.
The easiest solution is probably to create a local header file called iostream.h which just includes <iostream> and imports the namespace std. Then, in order for the compiler to allow #include <iostream.h> you add the local path to your include file search path. For g++, this works:
g++ -I local_folder [other flags] …
Incidentally, your remark about
… the deprecated "#include iostream.h"
isn’t quite correct: this isn’t deprecated because it has never been legal C++.
I'd take a step back and write another intermediate header you use everywhere instead that does something like:
#if defined(sun) || defined(__sun)
# if defined(__SVR4) || defined(__svr4__)
/* Solaris */
#include <iostream>
# else
/* SunOS */
#include "iostream.h"
# endif
#else
/* Sane, modern system */
#include <iostream>
#endif

Using TR1 libraries in GCC and MSVC

I would like to use the TR1 libraries that ship with modern versions of GCC and MSVC, but there are subtle differences: in GCC, I have to say
#include <tr1/memory>
std::tr1::shared_ptr<int> X;
while in MSVC I have to say
#include <memory>
std::shared_ptr<int> X;
I have two questions: 1) Does MSVC automatically operate in C++0x-mode (equivalent to GCC's std=c++0x), or does it also work in C++98/03 mode by default? 2) How can I unify the includes and namespaces? I was thinking about a preprocessor macro of the sort "INCLUDE_TR1(memory)" or something like that.
To clarify, I want to use the traditional, standard C++98/03; not C++0x (otherwise there'd be no problem).
I'd be most grateful for any suggestions!
VC++ 2010 only operates in C++0x mode; previous versions had no C++0x support. That said, much of the standard library in VC++ 2010 is is still based on TR1 (e.g. std::result_of<> uses the TR1 result_of protocol instead of being decltype-based); in fact, much of the standard library in VC++ 2010 is not actually defined in namespace std, but rather in namespace std::tr1 and pulled into namespace std with a using directive.
Use Boost.TR1 -- it will #include the appropriate headers according to your platform, or if your platform doesn't have TR1 support, #include the corresponding Boost implementations and pull them into namespace std::tr1 with using declarations.
VC++ 2010 always operates in C++0x mode, but the classes exist in both the std and std::tr1 namespaces. You’ll have to detect the compiler with an #if _MSC_VER to choose which headers to include (see this answer).
The Boost.TR1 library can automatically include your compiler’s headers and fill in any missing functionality using Boost. It might help.
OK, after having several inconsistent and unsurmountable problems with Boost.TR1, especially when trying to use GCC's native TR1 libraries, I decided to ditch Boost entirely and use a small #define workaround. Here is my "tr1.h":
#ifndef _TR1_INCLUDE_H
#define _TR1_INCLUDE_H
/** Usage: #include TR1INCLUDE(unordered_map)
**
** Configuration: Define HAVE_TR1_SUBDIR if you need #include <tr1/unordered_map>; otherwise we take #include <unordered_map>.
**
**/
#define QUOTE(arg) <arg>
#ifdef HAVE_TR1_SUBDIR
# define TR1IFY(arg) tr1/arg
#else
# define TR1IFY(arg) arg
#endif
#define TR1INCLUDE(arg) QUOTE(TR1IFY(arg))
#endif
Now I can just write my programs like this:
#include "tr1.h"
#include TR1INCLUDE(unordered_map)
The different versions of MSVC have the features they have. There is no way of turning them on or off.
Some of them might also have both tr1 and std versions of some features. With slightly different semantics!