__has_include Preprocessor Expression
Let's look at how we can use the `__has_include` preprocessor expression to check if a given header exists.
We'll cover the following
__has_include
If your code has to work under two different compilers, then you might experience two different sets of available features and platform-specific changes.
In C++17 you can use __has_include
preprocessor constant expression to check if a given header exists:
#if __has_include(<header_name>)
#if __has_include("header_name")
__has_include
was available in Clang as an extension for many years, but now it was added to the Standard. It’s a part of “feature testing” helpers that allows you to check if a particular C++ feature or a header is available.
If a compiler supports this macro, then it’s accessible even without the C++17 flag, that’s why you can check for a feature also if you work in C++11, or C++14 “mode”.
As an example, we can test if a platform has <charconv>
header that declares C++17’s low-level conversion routines:
Get hands-on with 1400+ tech skills courses.