Before using std::expected in a C++ header built as both C++17 and C++23, why test __cpp_lib_expected rather than __has_include(<expected>) or __cplusplus?
answer
- a header can exist but be empty
- __cplusplus names a mode, not a library
- <version> since C++20
- compare with >=, values can grow
basics
~20 s__has_include only says a header file exists; libc++ ships <expected> in every mode but declares nothing before C++23. __cplusplus names the language mode, not what the library implements. __cpp_lib_expected, from <version> or <expected>, says std::expected is usable.
solid answer
~40 sThree tests answer three different questions. `__has_include(<expected>)` (C++17) asks whether the header **file can be found**; with libc++ it is true under `-std=c++17` and `-std=c++20`, yet the header declares nothing there, and using `std::expected` fails. `__cplusplus` names the **language mode**: 201703L, 202002L or 202302L for C++17, C++20 and C++23. It says nothing about which library features a given implementation has finished, its C++26 value is provisional in today's compilers, and MSVC historically reports an older value unless `/Zc:__cplusplus` is passed. The **feature-test macro** `__cpp_lib_expected` is defined, to 202211L in the C++23 standard, exactly when the library provides `std::expected`. Get it by including `<version>` (C++20, guarded by `__has_include` for older modes) or `<expected>` itself, and compare with `>=`, because later revisions may raise the value.
code
cpp · 23 lines#if __has_include(<version>)
# include <version> // defines every __cpp_lib_* macro (C++20 header)
#endif
#include <cstdio>
#if defined(__cpp_lib_expected) && __cpp_lib_expected >= 202211L
# include <expected>
using LevelResult = std::expected<int, const char*>;
LevelResult parse_level(int raw) {
if (raw < 0) return std::unexpected("negative level");
return raw;
}
# define LOGX_HAS_EXPECTED 1
#else
# define LOGX_HAS_EXPECTED 0
#endif
int main() {
std::printf("__cplusplus=%ld expected=%d\n", static_cast<long>(__cplusplus), LOGX_HAS_EXPECTED);
#if LOGX_HAS_EXPECTED
std::printf("level=%d\n", parse_level(3).value());
#endif
}go deeper
Know that compilers define __cplusplus for the language mode and that standard library features have their own __cpp_lib macros.
Explain what each test answers: a header file's existence, the language mode, or an implemented feature, and why <version> exists.
Show the dual-mode header pattern, the libc++ case where the header exists but is empty, the provisional C++26 value and MSVC's lagging __cplusplus.
Decide how a library advertises and checks optional features across supported standards, centralising the tests in one configuration header instead of scattering them.
## Three tests, three questions A header that must compile as C++17 for one customer and C++23 for another needs to know whether `std::expected` exists before it uses it. Three preprocessor tools look relevant, and only one answers that question. | Test | What it really tells you | Problem as a feature check | |---|---|---| | `__has_include(<expected>)` (C++17) | a file with that name can be included | the file may exist but declare nothing in this mode | | `__cplusplus >= 202302L` | the compiler's language mode | says nothing about which library parts are finished; provisional values for C++26 | | `__cpp_lib_expected >= 202211L` | the library implements `std::expected` at that revision | none; this is its purpose | ## What the machine shows Compiling one probe file with Apple clang 21 and libc++ in four modes: | `-std=` | `__cplusplus` | `__has_include(<expected>)` | `__cpp_lib_expected` | |---|---|---|---| | `c++17` | 201703 | 1 | not defined | | `c++20` | 202002 | 1 | not defined | | `c++23` | 202302 | 1 | 202211 | | `c++2c` | 202400 | 1 | 202211 | libc++ ships `<expected>` in every mode and wraps its contents in a check for C++23, so under `-std=c++20` a file that includes `<expected>` and uses `std::expected` fails with `use of undeclared identifier 'std'`. A `__has_include` test would have said yes. ## Why __cplusplus is a coarse and sometimes wrong signal - It reports the **language mode**, not the library. Implementations ship features of a new standard over several releases, so a compiler in C++23 mode may still lack parts of the C++23 library. - For a standard still being finished, compilers use a **provisional value**. The C++26 final draft sets `202603L`; clang 21 in `-std=c++2c` reports 202400, and GCC 16's manual promises only an unspecified value strictly larger than `202302L` for `-std=c++26`. - MSVC has historically left `__cplusplus` at an older value unless `/Zc:__cplusplus` is passed, publishing the selected mode in `_MSVC_LANG` instead. CMake's own compiler detection compares the two for that reason, and vcpkg's Windows guidance recommends the switch. `__cplusplus` remains right for one job: telling C from C++ in a shared header, or requiring a minimum language mode outright. ## How feature-test macros work - **Language features** have predefined macros, no include needed: `__cpp_concepts` is `202002L` and `__cpp_if_constexpr` is `201606L` in the standard's table. - **Library features** have `__cpp_lib_*` macros, defined by the header **`<version>`** (added in C++20) and by the feature's own header, as the synopsis comment `// also in <expected>` says for `__cpp_lib_expected`. - Each value is a year and month, such as `202211L`, identifying the revision of the feature. The standard notes that future revisions might replace the values with greater ones, so compare with `>=` rather than `==`. The standard's own example of conditional inclusion combines the tools: include `<optional>` if `__has_include` finds it, then require `__cpp_lib_optional >= 201603` before using it. ## A pattern for a dual-mode header ```cpp #if __has_include(<version>) # include <version> #endif #if defined(__cpp_lib_expected) && __cpp_lib_expected >= 202211L # include <expected> # define LOGX_HAS_EXPECTED 1 #else # define LOGX_HAS_EXPECTED 0 #endif ``` 1. `__has_include(<version>)` protects modes that predate `<version>`; libc++ provides it even in C++17 mode, but other implementations may not. 2. `defined(...) &&` makes the intent explicit, since an undefined name would evaluate to 0 anyway. 3. One project macro, `LOGX_HAS_EXPECTED`, keeps the rest of the code free of repeated feature tests. GCC's manual adds a portability rule for `__has_include` itself: test `#if defined __has_include` in a separate directive first if the code must also build with compilers that lack the operator. ## Common mistakes - **Testing a library macro without including anything.** `__cpp_lib_*` macros come from `<version>` or the feature's header; in a file that includes neither, `#ifdef __cpp_lib_expected` is false even on a library that has it. - **Checking only `defined` when a revision matters.** Some features gained capabilities in later revisions, which raised their macro values; `#ifdef` accepts the oldest one. - **Testing the vendor instead of the feature.** `_LIBCPP_VERSION` or a compiler version number says which implementation and release you have, not whether this mode enables the feature. Keep vendor checks for working around a known bug in one release. ## Interview summary - `__has_include` finds files, `__cplusplus` names modes, `__cpp_lib_*` reports features. - Include `<version>` to get every library feature macro at once. - Compare with `>=`, and wrap the result in one project macro.
- Why include <version> rather than <expected> to read __cpp_lib_expected?<version> defines every library feature-test macro without pulling in any facility, so one cheap include answers all feature questions. Including <expected> also defines the macro, but you then include a header before you know it is useful. In modes before C++20, guard the <version> include with __has_include.
- What is the difference between __cpp_concepts and __cpp_lib_concepts?__cpp_concepts is a language feature-test macro, predefined by the compiler when it supports concepts syntax such as requires-clauses. __cpp_lib_concepts is a library macro, defined through <version> or <concepts>, when the standard library provides the concepts header's contents. A compiler can support the syntax while its library lags, so test the one your code needs.
- __has_include(<expected>) being true means std::expected can be used
- __cplusplus >= 202302L guarantees every C++23 library feature is available
- Feature-test macros should be compared with == to the documented value
- __cpp_lib macros are predefined without including any header
- Every compiler in C++26 mode reports __cplusplus as 202603L today
Assumes C++23 for std::expected and C++20 for the <version> header; __has_include exists since C++17, and C++26 __cplusplus values are provisional in current compilers.