C++ Reference ยท Preprocessor, Tools and Concepts
Feature-test macros
Reference for C++ feature-test macros: __cpp_ language macros, __cpp_lib_ library macros from <version>, __has_cpp_attribute and how to write portable fallbacks.
Feature-test macros tell your code, at preprocessing time, whether the compiler and standard library support one particular feature. They are more precise than checking __cplusplus: compilers adopt a standard feature by feature, so "C++23 mode" can still lack a specific library component, and "C++17 mode" may already offer a C++20 feature as an extension.
Language features use macros named __cpp_... (for example __cpp_concepts), library features use __cpp_lib_... (for example __cpp_lib_format) and are defined by including the lightweight <version> header (or the header of the feature itself). Each macro expands to a number, a year-and-month such as 202002L, which grows when the feature gains later improvements. Attributes are tested with __has_cpp_attribute(name) and headers with __has_include.
#include <version> #ifdef __cpp_lib_expected // std::expected is available #endif
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
The numbers the macros expand to are specific to each compiler and version, so print them only to see what you have. The naming pattern looks like this (read-only):
// language macros: __cpp_ + feature name __cpp_concepts __cpp_if_consteval // library macros: __cpp_lib_ + feature name (from <version>) __cpp_lib_ranges __cpp_lib_print // each expands to a number such as 202207L: bigger means a later revision
Syntax
#if __cpp_feature_name >= 202002L #ifdef __cpp_lib_feature_name #if __has_cpp_attribute(attribute_name) // value: non-zero if supported, usually a year-month #if __has_include(<header>)
| Macro family | Description |
|---|---|
__cpp_<feature> |
Language feature. Always available; no include needed. Examples: __cpp_concepts, __cpp_coroutines, __cpp_constexpr, __cpp_if_consteval, __cpp_explicit_this_parameter, __cpp_modules. |
__cpp_lib_<feature> |
Library feature. Defined by <version> or the feature's own header. Examples: __cpp_lib_format, __cpp_lib_ranges, __cpp_lib_expected, __cpp_lib_generator, __cpp_lib_jthread. |
__has_cpp_attribute(name) |
Preprocessor-time test for [[name]]; also [[gnu::name]] forms with a namespace. |
__has_include(h) |
Test for a header (see #include). |
<version> |
Header that only defines the library feature-test macros and implementation information. |
Errors: none for testing. Using __has_cpp_attribute itself on a compiler that lacks it is an error unless guarded with #ifdef __has_cpp_attribute.
Usage notes
- Test the feature you need, not the standard version.
#ifdef __cpp_lib_rangesis more accurate than#if __cplusplus >= 202002L, which can be true while a library component is still missing. - Include
<version>first (C++20) to get all library macros cheaply; before C++20, include the specific header of the feature. - Compare with
>=when you need a specific revision: for example__cpp_lib_format >= 202106Lfor the C++20 text, newer values for later fixes. - Always provide a fallback. The point of the macros is graceful degradation: use the new facility if present, else a small portable replacement.
- They don't prove runtime behavior. A macro says the feature exists in the headers, not that it is bug free in this compiler version.
- Not every feature has a macro, and not every compiler defines them for features it supports as an extension. Defined values can differ between compilers for the same standard.
- Prefer
if constexprandrequireswhen you can detect the capability with C++ itself (for example "does this type have a.size()"). Feature-test macros are for the compiler and library, not for types. - On our runner (GCC 15,
-std=c++23)std::expected,std::format,std::print,std::generatorandstd::flat_mapare available.std::mdspanis not available,import std;is not supported, and C++26 features such asinplace_vectorand reflection are missing, so their macros are undefined there. - Full list: the table of all macros is in the standard (the
[support.limits]section) and on cppreference, which is a fine place to look up names; do not copy the numbers into code without testing them.