What is new in C++20?
- Concepts: named rules for templates.
- Ranges and views: pipelines like
v | views::filter(...) | views::transform(...). - Modules: a modern replacement for header files.
- Coroutines: functions which can pause and continue.
- std::format: type safe formatting.
- Spaceship operator
<=>: all comparisons with one function. - std::span, std::jthread, designated initializers and more.
#include <format>
#include <iostream>
#include <ranges>
#include <span>
#include <vector>
struct Point { int x; int y; };
int main() {
Point p{.x = 1, .y = 2}; // designated initializers
std::vector<int> v = {1, 2, 3, 4, 5, 6};
auto evens = v | std::views::filter([](int n) { return n % 2 == 0; });
std::span<const int> view{v};
std::cout << std::format("{} {} {} {}\n", p.x + p.y, *evens.begin(), view.size(), (1 <=> 2) < 0);
}
Output
3 2 6 true
See Modern C++ Features.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |