Lambda Expressions¶
Map of STL Algorithms¶
Lambda Expressions¶
Map of STL Algorithms¶
The World Map of C++ STL Algorithms
Lambda¶
Lambda expression creates anonymous function object
1 |
|
Minimal lambda: [] {}
With return type:
1 |
|
Lambdas without capture (stateless) are compatible with plain C functions
1 2 3 4 5 |
|
- Default capture by reference may lead to dangling references to destroyed scope.
- Default capture by values may lead to dangling pointers to destroyed scope (through this).
- Default capture by values does not capture static variables, but uses them by reference instead.
- Default capture is safe when used locally, but even then someone may copy-paste them into unsafe environment
- \Rightarrow explicit capture forces to think, hence safer
Lambda Init¶
- Capture ownership (unique_ptr, future, thread)
[ var = initExpr ] (...) {...}
- Left of
=
is inner scope, right of=
is outer scope
1 2 3 4 5 6 7 |
|
Last update:
June 15, 2021