The Evolution of C++
Modern C++ has evolved significantly from its roots. C++17, C++20, and C++23 bring powerful features that make C++ more expressive, safer, and easier to use while maintaining its performance characteristics.
C++17 Highlights
- Structured bindings: Decompose objects into components
- if constexpr: Compile-time conditionals
- std::optional, std::variant: Better value semantics
- Fold expressions: Simplified variadic templates
// Structured bindings
std::map<std::string, int> ages = {{"Alice", 30}, {"Bob", 25}};
for (const auto& [name, age] : ages) {
std::cout << name << " is " << age << " years old\n";
}
// if constexpr
template<typename T>
auto get_value(T t) {
if constexpr (std::is_pointer_v<T>) {
return *t;
} else {
return t;
}
}
C++20 Game Changers
Concepts
Constrain templates with readable syntax:
template<typename T>
concept Numeric = std::integral<T> || std::floating_point<T>;
template<Numeric T>
T add(T a, T b) {
return a + b;
}
// Or with requires clause
template<typename T>
requires std::integral<T>
T multiply(T a, T b) {
return a * b;
}
Ranges
Composable algorithms and views:
#include <ranges>
#include <vector>
std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto result = numbers
| std::views::filter([](int n) { return n % 2 == 0; })
| std::views::transform([](int n) { return n * n; })
| std::views::take(3);
// result: 4, 16, 36 (lazily evaluated)
Coroutines
Asynchronous programming primitives:
#include <coroutine>
generator<int> fibonacci() {
int a = 0, b = 1;
while (true) {
co_yield a;
auto next = a + b;
a = b;
b = next;
}
}
C++23 Additions
- std::print/std::println: Type-safe formatting output
- std::expected: Error handling without exceptions
- Deducing this: Simpler CRTP patterns
- std::mdspan: Multidimensional array views
// std::print (C++23)
std::print("Hello, {}!\n", "World");
// std::expected
std::expected<int, std::string> parse(const std::string& s) {
try {
return std::stoi(s);
} catch (...) {
return std::unexpected("Invalid number");
}
}
← Back to Developer Resources