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/14.cpp at 2e9374873f80b2dc801c403889d200fa05f47f61 · threaderic/examples · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
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
2e9374873f80b2dc801c403889d200fa05f47f61
Breadcrumbs
examples
/
cpp
/
14.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
175 lines (136 loc) · 3.63 KB
2e93748
Breadcrumbs
examples
/
cpp
/
14.cpp
Copy path
Top
File metadata and controls
Code
Blame
175 lines (136 loc) · 3.63 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
static_assert
(__cplusplus ==
201402
,
"
"
);
#
include
<
utility
>
#
include
<
iostream
>
#
include
<
memory
>
#
include
<
chrono
>
#
include
<
tuple
>
#
include
<
string
>
#
include
<
cassert
>
#
include
<
algorithm
>
#
include
<
array
>
#
include
<
memory
>
using
namespace
std
;
/*
*
@file
@brief C++14
@defgroup CPP14 C++14 examples
https://en.wikipedia.org/wiki/C++14
https://isocpp.org/wiki/faq/cpp14-language
https://github.com/AnthonyCalandra/modern-cpp-features/blob/master/CPP14.md
@{
@defgroup lambda14 Lambda
https://en.cppreference.com/w/cpp/language/lambda
@{
*/
static
void
lambda_demo
()
{
//
Generic lambdas
//
auto before_generic_lambda = [](int x) { return x; };
auto
generic_lambda = [](
auto
x) {
return
x; };
auto
universal_size = [](
const
auto
& m) {
return
m.
size
(); };
auto
capture_initializers = [value =
1
] {
return
value; };
unique_ptr<
int
>
ptr
(
new
int
(
10
));
auto
capture_by_move = [value =
std::move
(ptr)] {
return
*value; };
}
/*
*
@}
@defgroup ded14 Deduction
@{
*/
//
template <typename T> T before_deduced_return_type(int a) { return a; }
auto
deduced_return_type
(
int
a)
{
return
a;
}
//
template <typename T> T& before_deduced_return_type_template(T& a) { return a; }
template
<
typename
T>
auto
&
deduced_return_type_template
(T& t)
{
return
t;
}
//
Returns a reference to a deduced type.
auto
deduced_return_type_lambda = [](
auto
& x) ->
auto
& {
return
deduced_return_type_template
(x);
};
/*
*
@}
@defgroup other14 Other
@{
https://en.cppreference.com/w/cpp/language/variable_template
https://en.cppreference.com/w/cpp/language/constexpr
https://en.cppreference.com/w/cpp/language/integer_literal
https://en.cppreference.com/w/cpp/language/function // decltype(auto)
https://en.cppreference.com/w/cpp/language/aggregate_initialization
https://en.cppreference.com/w/cpp/language/lambda
https://en.cppreference.com/w/cpp/thread/shared_timed_mutex
https://en.cppreference.com/w/cpp/thread/shared_lock
https://en.cppreference.com/w/cpp/utility/integer_sequence
https://en.cppreference.com/w/cpp/utility/exchange
https://en.cppreference.com/w/cpp/io/manip/quoted
// [[deprecated]]
*/
//
/ Binary literals, digit separators
auto
binary_literal =
0b0100'1100'0110
;
auto
integer_literal =
1'000'000
;
auto
floating_point_literal =
0.000'015'3
;
//
/ Standard user-defined literals
auto
str =
"
hello world
"
s;
//
auto deduces string
auto
dur = 60s;
//
auto deduces chrono::seconds
//
/ Tuple addressing via type
static
void
tuple_demo
()
{
tuple<string, string,
int
>
tuple_by_type
(
"
foo
"
,
"
bar
"
,
7
);
int
i = get<
int
>(tuple_by_type);
assert
(i ==
7
);
int
j = get<
2
>(tuple_by_type);
assert
(j ==
7
);
}
//
/ Template variables
namespace
template_variables
{
template
<
typename
T>
constexpr
T pi = T(
3.141592653589793238462643383
);
//
Usual specialization rules apply:
template
<>
constexpr
const
char
* pi<
const
char
*> =
"
pi
"
;
static
void
demo
()
{
assert
(pi<
int
> ==
3
);
assert
(
string
(pi<
const
char
*>) ==
"
pi
"
);
}
}
//
/ int explicit_return_type()
auto
implicit_return_type
()
{
return
1
;
}
void
dynamic_memory_14
()
{
//
https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique
auto
u = make_unique<
int
>(
1
);
assert
(u);
assert
(*u ==
1
);
}
//
/ Compare with @ref sort_11
void
sort_14
()
{
array<
int
,
10
> s = {
5
,
7
,
4
,
2
,
8
,
6
,
1
,
9
,
0
,
3
};
sort
(s.
begin
(), s.
end
(),
//
sort using a generic lambda expression
[](
auto
a,
auto
b)
{
return
a > b; }
);
}
//
/ @}
int
main
(
void
)
{
auto
x =
deduced_return_type
(
1
);
int
& y =
deduced_return_type_lambda
(x);
//
reference to `x`
assert
(&y == &x);
assert
(y ==
1
);
tuple_demo
();
template_variables::demo
();
implicit_return_type
();
dynamic_memory_14
();
sort_14
();
return
0
;
}
//
/ @}
You can’t perform that action at this time.