Modern C++ • C++20 baseline
C++23 Features and std::expected
Explore selected C++23 library improvements while checking compiler and standard-library support.
C++23 adds useful language and library features, but availability depends on both compiler and standard-library versions. std::expected represents either a value or an error and is useful when failure is an ordinary result. This lesson is optional; the main course otherwise targets C++20.
Key points
- Compile with an explicit C++23 mode and verify the library implements expected.
- Keep C++23 examples separate when your deployment baseline is C++20.
Standard note: This optional lesson requires C++23 compiler and library support. The core course uses C++20.
Example
#include <expected>
#include <string>
std::expected<int, std::string> parse_level(bool valid) {
if (!valid) {
return std::unexpected("invalid level");
}
return 3;
}
What to expect
Practice exercise
Check your compiler’s feature-test macros and standard-library documentation before using std::expected.
Key idea: Language mode alone does not guarantee every library feature is implemented.