Hi,
I've recently attempted to conditionally-include <string_view>, using the
<optional> example from P0061R1:
#if __has_include(<optional>)
# include <optional>
# define have_optional 1
#elif __has_include(<experimental/optional>)
# include <experimental/optional>
# define have_optional 1
# define experimental_optional 1
#else
# define have_optional 0
#endif
Unfortunately, GCC reports that the header is available even in C++14-mode,
but then #errors out when actually #including it, and there does not appear to
be any incentive to fix the situation:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79433
I'd like to suggest that, for all standard headers, C++17 mandates that
__has_include must not evaluate to 1 if the header is not availabe in that
version of the standard.
I know this is hairy, since the standard always assumes that all standard
features are available (that includes headers), and so wording would have to
reference future standards, but if you do nothing, then you might as well
remove the __has_include feature, because it becomes nearly impossible to use
it portably.
Suggested wording, to fix the situation at least for C++17+:
[....] has-include-expression evaluates to 1 if the search for the source
file succeeds, and to 0 if the search fails.
+ The search shall fail for all headers defined in future revisions of this
+ standard, and not already present in this version of the standard.
Or:
+ Implementations shall ensure that this search does not find headers added
+ only by future revisions of this standard.
This would not be an issue if all implementations supported SD-6, _and_ no
SD-6 macro, ever, was not either pre-defined or defined in a C++98 header,
such as cstddef. But right now, where the SD-6 macro for
experimental::string_view is defined in <experimental/string_view>,
__has_include returns true but #include fails, the whole SD-6 architecture,
even ignoring MSVC, is falling apart.
Thanks,
Marc
--
Marc Mutz <
marc...@kdab.com> | Senior Software Engineer
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
Tel:
+49-30-521325470
KDAB - The Qt, C++ and OpenGL Experts