-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp20_demo.cpp
More file actions
134 lines (108 loc) · 3.47 KB
/
Copy pathcpp20_demo.cpp
File metadata and controls
134 lines (108 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// c++cpp20_demo.cpp
#include <algorithm>
#include <chrono>
#include <format>
#include <iostream>
#include <numbers>
#include <ranges>
#include <span>
#include <vector>
// ---------- Feature 1: concepts ----------
template<typename T>
concept Numeric = std::is_arithmetic_v<T>;
template<Numeric T>
T average(const std::span<T> data)
{
T sum = 0;
for (T val : data)
sum += val;
return sum / static_cast<T>(data.size());
}
void demo_concepts()
{
std::cout << "\n=== concepts ===\n";
std::vector<double> doubles{1.1, 2.2, 3.3};
std::cout << std::format("Average: {:.2f}\n",
average(std::span(doubles))); // compile error if non-numeric!
// Fails at compile: average(std::vector<std::string>{"a", "b"})
}
// ---------- Feature 2: ranges (views, filter, transform) ----------
void demo_ranges()
{
std::cout << "\n=== ranges ===\n";
std::vector<int> vec{1, 2, 3, 4, 5, 6};
// Lazy composable views (no copies!)
auto evens = vec | std::views::filter([](int n) { return n % 2 == 0; })
| std::views::transform([](int n) { return n * n; });
for (int sq : evens) {
std::cout << sq << " "; // 4 16 36
}
std::cout << "\n";
// reverse order print
for (const int n : vec | std::views::reverse) {
std::cout << n << " "; // 6 5 4 3 2 1
}
}
// ---------- Feature 3: std::format ----------
void demo_format()
{
std::cout << "\n=== std::format ===\n";
std::cout << std::vformat("Pi ≈ {:.3f}, e ≈ {:.3f}\n",
std::make_format_args(std::numbers::pi_v<double>,
std::numbers::e_v<double>));
int x = 42, y = 1234567;
std::cout << std::format("x={:>5}, y={}\n", x, y);
}
// ---------- Feature 4: std::span (non-owning view) ----------
void print_span(std::span<const int> data)
{
std::cout << "Span size=" << data.size() << ": [";
for (size_t i = 0; i < data.size(); ++i) {
std::cout << data[i] << (i + 1 < data.size() ? ", " : "");
}
std::cout << "]\n";
}
void demo_span()
{
std::cout << "\n=== std::span ===\n";
std::vector<int> vec{10, 20, 30, 40};
print_span(vec); // full
print_span(std::span{vec}.subspan(1, 2)); // [20, 30]
}
// ---------- Feature 5: chrono calendar (date literals) ----------
void demo_chrono()
{
std::cout << "\n=== chrono calendar ===\n";
using namespace std::chrono;
auto now = sys_days{2026y / January / 29} + 15h + 30min; // C++20 literals
std::cout << std::format("Vancouver time: {} ({})\n",
now,
year_month_day{
floor<days>(now)}); // use day-precision for year_month_day
}
// ---------- Feature 6: constinit & consteval ----------
constinit int global_counter = 0; // guaranteed init before main (thread-safe)
consteval int square(int x)
{ // always compile-time
return x * x;
}
constexpr int arr[] = {square(3), square(4)}; // consteval used here
void demo_const()
{
std::cout << "\n=== constinit & consteval ===\n";
std::cout << "arr[0]=" << arr[0] << ", arr[1]=" << arr[1] << "\n";
std::cout << "global_counter=" << ++global_counter << "\n";
}
// ---------- main ----------
int main()
{
std::cout << "C++20 feature tour\n";
demo_concepts();
demo_ranges();
demo_format();
demo_span();
demo_chrono();
demo_const();
std::cout << "\nDone.\n";
return 0;
}