Using C++20 and C++23
Using C++20 and C++23
Start adopting modern language and library features that pay off quickly.
Using C++20 and C++23
Start adopting modern language and library features that pay off quickly.
std::span for non-owning array viewsstd::jthread for safer thread lifetime managementstd::expected for explicit success-or-error return valuesstd::spanvoid print_all(std::span<const int> values) {
for (int value : values) {
std::cout << value << '\n';
}
}
template <typename T>
concept Printable = requires(T value) {
std::cout << value;
};
Add modern features where they improve readability immediately. Do not rewrite stable code just to chase newer syntax.
std::optional, std::variant, and std::expected for value-based APIsstd::format and std::print for cleaner text outputstd::source_location for diagnostics without manual file and line plumbingstd::chrono calendar/time-zone facilities when you need real-world dates and timesstd::expectedstd::expected<int, std::string> parse_count(std::string_view text) {
if (text.empty()) {
return std::unexpected("empty input");
}
return 42;
}
These two features are real parts of modern C++, but they require more build-system and library support than features like std::span or concepts. Adopt them deliberately rather than mechanically.
Start with concepts, ranges, std::span, std::string_view, std::format, and std::expected. Move to modules and coroutines when your toolchain, CI, and surrounding libraries are ready.