I agree with you that feature_test is described as not in the best way. In these cases, it's better to use Languages Standards. They are not publicly available for free, but there are working drafts:
(Links with draft can be out of date after 10 years, but I have provided the names of the documents to not violate StackOverflow policy.)
For example, I am using a very recent version of GCC with -std=c++17 for the feature __cpp_lib_hardware_interference_size. The macro is undefined, which I take to mean GCC doesn't have the feature...
Not exactly. Let's be careful. This inline constexpr constant is available from C++2017 hardware_destructive_interference_size, but the problem is that feature_test feature_test abilities with __cpp_*
has been introduced only in C++2020. You will not find any of __cpp_*
Marcoses in [1]. What is not supported is not a feature in C++2017 from GCC Toolchain, but the ability to test a feature with feature test macros by itself.
To test for a feature, cppreference mentions these feature test macros...
You can test the feature if feature testing is supported. And it is supported only from C++2020 (officially).
In all versions of C/C++, the compiler reserved the following names:
- Starting with a double underscore
__
- Names that start with a single underscore
_
followed by an uppercase letter.
You can try to use this mechanism in C++98/03/11/14/17, but the macro may be undefined, and in #if
statement the undefined macro will be expanded as 0
value.
But it's very unreliable.
They are defined as something like 201606, which I believe is a version of C++, not of the compiler...
Be careful. Section 15.11 [2] does not state that exactly. However, I hope toolchain implementation will define exactly what you said. Unfortunately, the (working draft of) standard does not force that.
Dear @J. Calleja
So, you can check if the feature exists by checking if the macro is defined.
Yes, but if you use C++2020, not C++98/03/11/14/17.
Or you can check for the feature version based on the macro value.
I'd like to add an extra comment. Please be aware of the following statement from [2], Section 15.11, p.455:
"Future versions of this International Standard might replace the values of these macros with greater values."
And I don't see anything which mentions this inside cppreference feature_test.
If you want to produce compatible code use constructions like:
#if __cpp_char8_t >= 201811L
///
#endif