正常
Lambda Expressions (C++11)
In a Nutshell
A lambda lets us define an anonymous function object in-place right in our code, and is commonly used to pass short snippets of logic as arguments to algorithms or callbacks.
Header
None (language feature)
Core API Quick Reference
| Operation | Signature | Description |
|---|---|---|
| Basic form | [captures](params) { body } | Basic syntax, generates a closure type |
| No-parameter lambda | [captures] { body } | Shorthand that omits the parameter list |
| Capture by value | [x, y] | Captures variables by copying their values |
| Capture by reference | [&x, &y] | Captures variables by reference |
| Capture all by value | [=] | Captures all used automatic variables by value |
| Capture all by reference | [&] | Captures all used automatic variables by reference |
| Mutable lambda | [captures](params) mutable { body } | Allows modifying the copies captured by value |
| Generic lambda | [captures](auto a, auto b) { body } | Parameters use auto, templated operator() |
| Explicit template parameters | [captures]<typename T>(T a) { body } | C++20, explicitly specifies a template parameter list |
| Static lambda | [captures](params) static { body } | C++23, operator() is a static member function |
Minimal Example
C++
#include <algorithm>
#include <vector>
#include <iostream>
// Standard: C++11
int main() {
std::vector<int> v = {3, 1, 4, 1, 5};
int threshold = 3;
auto count = std::count_if(v.begin(), v.end(),
[threshold](int x) { return x > threshold; });
std::cout << count << "\n"; // Output: 2
}Embedded Suitability: High
- The closure type is generated at compile time, with no heap allocation overhead and zero additional runtime cost
- Replaces function pointers and hand-written functors, making callback code more compact and more readable
- Watch out for the lifetime risks of reference captures in asynchronous or interrupt contexts; value capture is recommended for embedded callbacks
- C++14 generic lambdas let us write generic sorting/searching comparison logic without template overhead
Compiler Support
| GCC | Clang | MSVC |
|---|---|---|
| 4.5 | 3.1 | 19.0 |
See Also
Some content references cppreference.com, used under the CC-BY-SA 4.0 license