What is new in C++23?
- std::print and std::println: easy formatted output.
- std::expected: a value or an error, without exceptions.
- Deducing this: one member function for const and non const objects.
- std::mdspan: a view of multidimensional data.
- std::flat_map, std::flat_set, std::generator and more ranges features.
- if consteval,
std::unreachable,std::to_underlyingand others.
#include <expected>
#include <print>
#include <utility>
enum class Color { Red = 1, Green = 2 };
std::expected<int, std::string> half(int n) {
if (n % 2) return std::unexpected("odd number");
return n / 2;
}
int main() {
std::println("{}", half(10).value());
std::println("{}", half(7).error());
std::println("{}", std::to_underlying(Color::Green));
}
Output
5
odd number
2
All of these compile with GCC 16 and -std=c++23. See Modern C++ Features.
Learn more about the similar topics:
| Tutorials |
|---|
| C++ Lambda Expressions |
| C++ Move Semantics and the Rule of Five |
| Modern C++ Features: ranges, optional, variant and format |
| C++ Multithreading: thread, mutex and async |
| Exercises & Assignments |
|---|
| Filter and sum numbers with algorithms and ranges |