C++ features by examples
14.cpp
Go to the documentation of this file.
1
15static_assert(__cplusplus == 201402, "");
16
17#include <bits/stdc++.h>
18
19using namespace std;
20
26#define static_assert(a) static_assert(a, "")
27static_assert(__cpp_return_type_deduction);
28
30{
31 return 1;
32}
33
34// template <typename T> T before_deduced_return_type(int a) { return a; }
36{
37 return a;
38}
39
40// template <typename T> T& before_deduced_return_type_template(T& a) { return a; }
41template <typename T> auto& deduced_return_type_template(T& t)
42{
43 return t;
44}
45
46// Returns a reference to a deduced type.
47auto deduced_return_type_lambda = [](auto& x) -> auto& {
49};
50
52{
54 auto x = deduced_return_type(1);
55 int& y = deduced_return_type_lambda(x); // reference to `x`
56 assert(&y == &x);
57 assert(y == 1);
58}
59
61
92
95
96static_assert(__cpp_variable_templates);
97
99template<typename T>
100T pi = T(3.141592653589793238462643383);
101
102// String pi
103// Usual specialization rules apply:
104template<>
105const char* pi<const char*> = "pi";
106
107static void demo()
108{
109 assert(pi<int> == 3);
110 assert(string(pi<const char*>) == "pi");
111
112 auto binary_literal = 0b0100'1100'0110;
113
114 auto integer_literal = 1'000'000;
115
116 auto floating_point_literal = 0.000'015'3;
117
119
120 auto str = "hello world"s; // auto deduces string
121 auto dur = 60s; // auto deduces chrono::seconds
122
123}
124
126{
127 static_assert(is_null_pointer<decltype(nullptr)>::value);
128 static_assert(is_null_pointer<nullptr_t>::value);
129 static_assert(__cpp_decltype);
130 static_assert(is_integral<int>());
131
132 // Tuple addressing via type
133 tuple<string, string, int> tuple_by_type("foo", "bar", 7);
134
135 int i = get<int>(tuple_by_type);
136 assert(i == 7);
137
138 int j = get<2>(tuple_by_type);
139 assert(j == 7);
140
141 // https://en.cppreference.com/w/cpp/language/auto
142 int a = 0;
143 static_assert(__cpp_decltype_auto);
144 decltype(auto) a_copy = a;
145 assert(&a_copy != &a);
146 decltype(auto) a_ref = (a);
147 assert(&a_ref == &a);
148
149 // https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique
150 //auto u = unique_ptr<int>(new int(1));
151 auto u = make_unique<int>(1);
152 assert(u);
153 assert(*u == 1);
154 auto ua = make_unique<int[]>(3);
155}
156
158
163static void lambda_14()
164{
165 // Generic lambdas
166 // auto before_generic_lambda = [](int x) { return x; };
167 static_assert(__cpp_generic_lambdas);
168 auto generic_lambda = [](auto x) { return x; };
169
170 auto universal_size = [](const auto& m) { return m.size(); };
171
172 static_assert(__cpp_init_captures);
173 auto capture_initializers = [value = 1] { return value; };
174
175 assert(capture_initializers() == 1);
176
177 auto mutable_lambda = [value = 1] () mutable { return ++value; };
178
179 assert(mutable_lambda() == 2);
180 assert(mutable_lambda() == 3);
181
182 // capture initialization can change context
183 int c = 0;
184 auto change_c = [value = ++c] { };
185 assert(c == 1);
186
187 unique_ptr<int> ptr(new int(10));
188 auto capture_by_move = [value = move(ptr)] { return *value; };
189}
190
192
194{
195 array<int, 10> s = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
196 sort(s.begin(), s.end(),
197 // sort using a generic lambda expression
198 [](auto a, auto b)
199 { return a > b; }
200 );
201}
202
204
205int main(void)
206{
208 demo();
209 sort_14();
210 types_14();
211 lambda_14();
212}
int main(void)
Definition: 14.cpp:205
auto & deduced_return_type_template(T &t)
Definition: 14.cpp:41
auto deduced_return_type(int a)
Definition: 14.cpp:35
auto implicit_int_return_type()
Definition: 14.cpp:29
auto deduced_return_type_lambda
Definition: 14.cpp:47
void return_type_deduction_demo()
Definition: 14.cpp:51
static void lambda_14()
Definition: 14.cpp:163
void sort_14()
Compare with lambda::sort_11.
Definition: 14.cpp:193
int & a_ref(int &a)
https://en.cppreference.com/w/cpp/language/reference
Definition: 03.cpp:209
static void demo()
Definition: 14.cpp:107
T pi
Binary literals, digit separators.
Definition: 14.cpp:100
const char * pi< const char * >
Definition: 14.cpp:105
void types_14()
Definition: 14.cpp:125