标签:optional ons obj exception variables object scope var each
Emerged since c++11, lambda expression/function is an unnamed function object capable of capturing variables in scope.
1. syntax of a lambda expression
[
captures ]
<tparams>(optional)(c++20) (
params )
specifiers exception attr ->
ret requires(optional)(c++20){
body }
[
captures ]
(
params )
->
ret {
body }
[
captures ]
(
params )
{
body }
[
captures ]
{
body }
examples:
[](){} //barebone lambda
[](){}() // immediately execute a lambda
auto print = [](const int& n) { n = n+1; std::cout << " " << n; };
std::for_each(v.begin(), v.end(), print); // caller of the lambda expression
std::for_each(v.begin(), v.end(), [](int &n){ n++; });
标签:optional ons obj exception variables object scope var each
原文地址:https://www.cnblogs.com/sarah-zhang/p/12075687.html