Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
examples/cpp/20.cpp at develop · threaderic/examples · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
threaderic
examples
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
develop
Breadcrumbs
examples
/
cpp
/
20.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
343 lines (253 loc) · 6.71 KB
develop
Breadcrumbs
examples
/
cpp
/
20.cpp
Copy path
Top
File metadata and controls
Code
Blame
343 lines (253 loc) · 6.71 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
*
@file
@brief C++20
@defgroup CPP20 C++20 examples
https://en.wikipedia.org/wiki/C++20
https://github.com/AnthonyCalandra/modern-cpp-features/blob/master/CPP20.md
https://en.cppreference.com/w/cpp/20
@{
*/
static_assert
(__cplusplus >=
201707
);
#
if
__has_include (<bit>)
#
include
<
bit
>
#
endif
#
include
<
vector
>
#
include
<
cmath
>
#
include
<
cassert
>
#
include
<
vector
>
#
include
<
map
>
#
include
<
memory
>
#
include
<
thread
>
#
include
<
sstream
>
#
include
<
experimental/array
>
#
if
__has_include (<concepts>)
#
include
<
concepts
>
#
endif
using
namespace
std
;
/*
*
@defgroup lang20 Language
https://en.cppreference.com/w/cpp/language
@{
*/
#
if
__cpp_lib_three_way_comparison
static_assert
(
1
<=>
2
<
0
);
static_assert
(
2
<=>
1
>
0
);
static_assert
(
1
<=>
1
==
0
);
#
else
#
pragma
message("undefined __cpp_lib_three_way_comparison")
#
endif
#
if
__cpp_char8_t
//
/ https://en.cppreference.com/w/cpp/keyword/char8_t
char8_t
char8;
#
endif
//
/ https://en.cppreference.com/w/cpp/container/array/to_array
auto
to_array_demo = experimental::to_array(
"
foo
"
);
static_assert
(to_array_demo.size() ==
4
);
void
init_20
()
{
struct
point
{
int
x, y; };
struct
line
{ point a, b; };
//
https://en.cppreference.com/w/cpp/language/aggregate_initialization
#
if
__cplusplus > 201707
point p1 = { .
x
=
1
};
assert
(p1.
x
==
1
);
assert
(!p1.
y
);
point p2 { {} ,
2
};
assert
(p2.
y
==
2
);
assert
(!p2.
x
);
#
endif
line l1 = { };
assert
(!l1.
a
.
x
);
line l2 = {
1
,
2
};
assert
(l2.
a
.
x
==
1
);
assert
(l2.
a
.
y
==
2
);
line l3 = {
1
,
2
,
3
,
4
};
assert
(l3.
b
.
x
==
3
);
assert
(l3.
b
.
y
==
4
);
}
void
types_20
()
{
#
if
__cpp_lib_bit_cast
cout <<
typeid
(bit_cast<
double
>(
0
));
#
endif
}
void
dynamic_memory_20
()
{
//
/ Shared array
//
/ https://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared
auto
a = make_shared<
int
[
10
]>();
static_assert
(is_same_v<
decltype
(a.
get
()),
int
*>);
//
/ https://en.cppreference.com/w/cpp/container/map/contains
map<
int
,
int
> m = {{
2
,
3
}};
#
if
__cplusplus > 201707
assert
(m.
contains
(
2
));
#
endif
#
if
__cpp_lib_erase_if
//
/ [erase_if](https://en.cppreference.com/w/cpp/container/map/erase_if)
vector<
int
> v = {
11
,
12
,
13
};
assert
(
erase_if
(v, [](
int
a) {
return
a >=
13
;}) ==
1
);
assert
(v.
size
() ==
2
);
#
endif
}
namespace
lambda
{
#
if
__cpp_template_template_args
//
/ Lambda capture of parameter pack
template
<
typename
... Args>
auto
make_lambda_with_parameter_pack_capture
(Args&& ... args)
{
//
parameter pack capture:
return
[... args = forward<Args>(args)] {
return
(... + args);
};
}
#
endif
void
lambda_20
()
{
#
if
__cplusplus >= 201709
//
generic lambda, operator() is a template with two parameters
auto
glambda = []<
class
T
>(T a,
auto
&& b) {
return
a < b; };
assert
(
glambda
(
1
,
2
));
//
generic lambda, operator() is a template with one parameter pack
auto
f = []<
typename
...
Ts
>(Ts&& ...
ts
) {
return
1
;
};
assert
(
f
(
1
,
2
,
3
));
struct
point
{
int
x, y; };
auto
point_lambda = []<
class
T
=point>(T&& var) {};
point_lambda
({
1
,
2
});
#
if
__cpp_template_template_args
assert
(
make_lambda_with_parameter_pack_capture
(
1
,
2
,
3
)() ==
6
);
#
endif
#
endif
}
}
using
namespace
lambda
;
//
/ @} lang20
/*
*
@defgroup templ20 Template
https://en.cppreference.com/w/cpp/language/templates
@{
*/
#
if
__cpp_concepts
/*
*
@}
@defgroup conc20 Concepts
https://en.wikipedia.org/wiki/Concepts_(C++)
https://en.cppreference.com/w/cpp/language/constraints
https://en.cppreference.com/w/cpp/header/concepts
@{
*/
/*
*
@defgroup req20 'Requires' clause and expression
@{
*/
//
Using [requires](https://en.cppreference.com/w/cpp/keyword/requires)
template
<
typename
T>
requires
is_integral_v<T> T
constexpr
requires_demo
(T a) {
return
a +
1
; }
static_assert
(requires_demo(
1
) ==
2
);
/*
*
requires-clause can be after function declaration and supports template overloading
*/
template
<
typename
T>
auto
constexpr
requires_demo
(T && a) requires is_same_v<T, double> {
return
2
; }
static_assert
(requires_demo(
0.1
) ==
2
);
//
Annotated example of complex requirement
template
<
class
T
>
requires
//
requires-clause
is_signed_v<T> || (is_unsigned_v<T> && ! is_void_v<
void
>)
//
constraint expression
void
complex_requirement_demo
() { }
//
/ Annotated example of requires-expression
template
<
class
T
>
requires
//
requires-clause
requires
()
//
requires-expression
{
true
;}
//
unevaluated requirements sequence
void
requires_expression_demo
() { }
template
<
class
T
>
requires
requires
(T a) { a /
0
; }
auto
constexpr
what
(T a) {
return
1
; }
static_assert
(what(
1
) ==
1
);
template
<
class
T
>
requires
requires
(T a) { a[
0
]; }
auto
constexpr
what
(T a) {
return
2
; }
static_assert
(what(
"
2
"
) ==
2
);
//
/ @}
/*
*
@defgroup conc_def_20 Concept definitions
@{
*/
//
/ trivial concepts as assignment
template
<
typename
T>
concept
truism =
true
;
#
if
!__cpp_lib_concepts
//
/ short concept definition from a constraint directly
template
<
class
T
>
concept
integral = is_integral_v<T>;
#
endif
/*
*
defining concept with requires-expression
*/
template
<
class
T
>
concept
integral_req_ct =
requires
(T a) { is_integral_v<T>; };
template
<integral_req_ct T>
constexpr
T
_inc2
(T a) {
return
a +
1
; }
static_assert
(_inc2(
1
) ==
2
);
/*
*
https://en.cppreference.com/w/cpp/language/constraints#Compound_Requirements
https://en.cppreference.com/w/cpp/concepts/convertible_to
*/
template
<
typename
T>
concept
compound_requirements =
requires
(T x) {
{x +
1
} -> convertible_to<
bool
>;
{x *
2
} -> same_as<
int
>;
};
//
/ @}
//
/ @}
#
else
#
pragma
message("undefined __cpp_concepts")
#
endif
#
if
__cpp_lib_ranges
#
include
<
ranges
>
//
/ [ranges](https://en.cppreference.com/w/cpp/ranges)
struct
ranges_20
{
ranges_20
() {
auto
data = {
0
,
1
,
2
,
3
};
//
/ [reverse_view](https://en.cppreference.com/w/cpp/ranges/reverse_view)
ranges::reverse_view rv {data};
auto
r = data | views::reverse;
//
r is kind of transform_view
vector
result
(r.
begin
(), r.
end
());
assert
(
vector
(rv.
begin
(), rv.
end
()) == result);
assert
((result == vector{
3
,
2
,
1
,
0
}));
}
} ranges_20;
#
else
#
pragma
message("undefined __cpp_lib_ranges")
#
endif
/*
*
@defgroup other20 Other
@{
TODO:
https://en.cppreference.com/w/cpp/language/range-for
https://en.cppreference.com/w/cpp/language/coroutines
https://en.cppreference.com/w/cpp/language/constinit
*/
//
/ @}
#
include
<
experimental/source_location
>
//
/ [source_location](https://en.cppreference.com/w/cpp/utility/source_location)
struct
location_20
{
location_20
() {
auto
l =
experimental::source_location::current
();
basic_stringstream<
char
> buff;
buff << l.
file_name
() <<
"
:
"
<< l.
line
() <<
"
:
"
<< l.
column
() << l.
function_name
();
assert
(buff.
str
().
length
());
}
} location_20;
int
main
()
{
init_20
();
types_20
();
dynamic_memory_20
();
lambda_20
();
#
if
__cpp_lib_jthread
jthread
t
([]{ });
#
endif
}
//
/ @}
/*
*
@mainpage
@ref CPP20
@ref CPP17
@ref CPP14
@ref CPP11
@ref CPP03
*/
You can’t perform that action at this time.