Easily find feature test macros with the <version> header
C++ has provided some form of feature test macros for as long as new features have been added. Beginning with C++20, the process is standardized, and all library feature test macros have been added to the <version>
header. This will make it much easier to test for a new feature in your code.
This is a useful feature and it's very simple to use.
How to do it…
All feature test macros begin with the prefix __cpp_
. Library features begin with __cpp_lib_
. Language feature test macros are typically defined by the compiler. Library feature test macros are defined in the new <version>
header. Use them as you would any other preprocessor macro:
#include <version> #ifdef __cpp_lib_three_way_comparison # include <compare> #else # error Spaceship has not yet landed #endif
In some cases, you can use the __has_include
preprocessor operator (introduced...