C++ features by examples
Collaboration diagram for Lambda:

Functions

static void lambda_14 ()
 
void sort_14 ()
 Compare with lambda::sort_11. More...
 

Detailed Description

https://en.cppreference.com/w/cpp/language/lambda

Function Documentation

◆ lambda_14()

static void lambda_14 ( )
static

Definition at line 163 of file 14.cpp.

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}
Here is the caller graph for this function:

◆ sort_14()

void sort_14 ( )

Compare with lambda::sort_11.

Definition at line 193 of file 14.cpp.

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}
Here is the caller graph for this function: