Phần 1
1. Goal
After learning lambda capture by value, you should understand:
1. Why lambdas need captures.
2. What capture by value means.
3. The difference between [] and [x].
4. Common mistakes when capturing by value.
5. How capture by value is useful in real-world C++ code.Phần 2
2. What Problem Does It Solve?
A basic lambda with [] cannot use local variables outside the lambda.
int threshold = 1000;
auto isLarge = [](int quantity) {
return quantity > threshold; // Error
};Capture by value allows the lambda to copy outside variables into itself.
int threshold = 1000;
auto isLarge = [threshold](int quantity) {
return quantity > threshold;
};Main idea:
Capture by value gives the lambda its own copy of outside variables.Phần 3
3. Minimum Syntax
Capture one variable by value:
[x](int value) {
return value > x;
};Capture multiple variables by value:
[a, b](int x) {
return x > a && x < b;
};Capture all used outside variables by value:
[=](int x) {
return x > threshold;
};Basic meaning:
[] = capture nothing
[x] = copy x into the lambda
[a, b] = copy a and b into the lambda
[=] = copy all used outside variablesPhần 4
4. Important Behavior
Capture by value copies the variable at the moment the lambda is created.
int threshold = 1000;
auto isLarge = [threshold](int quantity) {
return quantity > threshold;
};
threshold = 2000;The lambda still uses the old copied value: 1000.
Key idea:
Changing the original variable later does not change the lambda's copied value.Phần 5
5. Common Mistakes
5.1. Forgetting to capture
int threshold = 1000;
auto isLarge = [](int quantity) {
return quantity > threshold;
};This is wrong because threshold is outside the lambda and was not captured.
5.2. Expecting captured values to update automatically
int threshold = 1000;
auto isLarge = [threshold](int quantity) {
return quantity > threshold;
};
threshold = 2000;The lambda still uses the copied value 1000.
5.3. Capturing large objects by value
[orders]() {
// copies the whole orders container
};This may be expensive if orders is large.
For large objects, be careful. Capture by value is safe, but it can cost memory and time.
Phần 6
6. When Should You Use Capture by Value?
Use capture by value when:
1. You need outside variables inside a lambda.
2. The lambda should keep a stable snapshot of those variables.
3. The captured variables are small.
4. You want safer code that does not depend on later changes.Avoid it when:
1. The captured object is very large.
2. You need the lambda to see updated values.
3. You need to modify the original variable.Phần 7
7. Notes for Trading / Performance-Sensitive Code
Trading code often uses thresholds:
Large order threshold
Minimum price
Maximum risk limit
Target symbolCapture by value is useful when the lambda should use a stable rule:
int threshold = 1000;
auto isLargeOrder = [threshold](const Order& order) {
return order.quantity > threshold;
};This means the lambda keeps the threshold value that existed when it was created.
Phần 8
8. End-of-Day Checklist
You should remember:
[] = capture nothing
[x] = capture x by value
[a, b] = capture a and b by value
[=] = capture all used outside variables by valueYou should be able to answer:
1. Why does [] not allow using outside variables?
2. What does [threshold] mean?
3. When is the captured value copied?
4. Does changing the original variable update the lambda?
5. Why can capturing large objects by value be expensive?Phần 9
9. Conclusion
Capture by value lets a lambda use outside variables by copying them.
The key idea:
Capture by value gives the lambda a snapshot of outside variables.
It is safe and clear, but can be expensive for large objects.:::
Mã mẫu hoàn chỉnh
cpp11/8_lambda capture by value/main.cpp
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
struct Order {
int id;
std::string symbol;
int quantity;
};
int main() {
std::vector<Order> orders{
{101, "AAPL", 100},
{102, "MSFT", 1500},
{103, "NVDA", 800}
};
int threshold = 1000;
auto isLargeOrder = [threshold](const Order& o) {
return o.quantity > threshold;
};
threshold = 2000;
int cnt = 0;
for (const auto& order : orders) {
if (isLargeOrder(order)) {
++cnt;
}
}
std::cout << "Large order count = " << cnt << '\n';
return 0;
}Tự kiểm tra · không chấm điểm
Bạn đã hiểu những điểm nào?
Đã tự kiểm tra 0/5 mục