Phần 1
1. Goal
After learning lambda capture by reference, you should understand:
1. What capture by reference means.
2. How it differs from capture by value.
3. When to use [&x] and [&].
4. Common mistakes with dangling references.
5. How reference capture is used in real-world C++ code.Phần 2
2. What Problem Does It Solve?
Capture by value gives the lambda a copy of an outside variable.
Capture by reference lets the lambda use the original variable directly.
int total = 0;
auto add = [&total](int x) {
total += x;
};Main idea:
Capture by reference allows a lambda to read or modify the original outside variable.Phần 3
3. Minimum Syntax
Capture one variable by reference:
[&x]() {
x += 1;
};Capture multiple variables by reference:
[&a, &b]() {
a += 1;
b += 1;
};Capture all used outside variables by reference:
[&]() {
total += 1;
};Basic meaning:
[] = capture nothing
[x] = capture x by value
[&x] = capture x by reference
[=] = capture all used outside variables by value
[&] = capture all used outside variables by referencePhần 4
4. Capture by Value vs Capture by Reference
Capture by value:
int threshold = 1000;
auto f = [threshold]() {
return threshold;
};
threshold = 2000;The lambda still sees 1000.
Capture by reference:
int threshold = 1000;
auto f = [&threshold]() {
return threshold;
};
threshold = 2000;The lambda sees 2000.
Summary:
Capture by value = snapshot copy
Capture by reference = access original variablePhần 5
5. Common Mistakes
5.1. Dangling reference
Do not return a lambda that captures a local variable by reference.
auto makeLambda() {
int x = 10;
return [&x]() {
return x;
};
}After the function ends, x no longer exists. The lambda holds a broken reference.
5.2. Accidental modification
int threshold = 1000;
auto f = [&threshold]() {
threshold = 2000;
};Because the lambda captures by reference, it modifies the original variable.
5.3. Overusing [&]
auto f = [&]() {
// uses many outside variables
};This can make it unclear which variables the lambda depends on or modifies.
Prefer explicit captures when clarity matters:
auto f = [&total, threshold]() {
// total by reference, threshold by value
};Phần 6
6. When Should You Use Capture by Reference?
Use capture by reference when:
1. You need to modify an outside variable.
2. You want the lambda to see updated values.
3. You want to avoid copying a large object.
4. The lambda will not outlive the referenced variables.Avoid it when:
1. The lambda may outlive the referenced variables.
2. You want a stable snapshot.
3. Accidental modification would be dangerous.Phần 7
7. Notes for Trading / Performance-Sensitive Code
Trading code may use reference capture to accumulate results:
Total volume
Total notional
Risk counters
StatisticsExample idea:
double totalNotional = 0.0;
auto addNotional = [&totalNotional](const Trade& trade) {
totalNotional += trade.price * trade.quantity;
};This modifies the original totalNotional.
Be careful with lambdas stored for later execution, callbacks, or threads.
Phần 8
8. End-of-Day Checklist
You should remember:
[&x] = capture x by reference
[&] = capture all used outside variables by referenceYou should be able to answer:
1. What is the difference between [x] and [&x]?
2. When does capture by reference see updated values?
3. Why can dangling references be dangerous?
4. When should you avoid [&]?
5. Why is reference capture useful for accumulation?Phần 9
9. Conclusion
Capture by reference lets a lambda access the original outside variables.
The key idea:
Use capture by reference when you need shared access or modification.
Be careful with lifetime and accidental mutation.:::
Mã mẫu hoàn chỉnh
cpp11/9_lambda capture by reference/main.cpp
#include <iostream>
#include <string>
#include <vector>
struct Trade {
std::string symbol;
double price;
int quantity;
};
int main() {
std::vector<Trade> trades{
{"AAPL", 191.25, 100},
{"MSFT", 420.50, 50},
{"NVDA", 875.10, 10}
};
int totalVolume = 0;
double totalNotional = 0.0;
auto accumulate = [&totalVolume, &totalNotional](const Trade& t) {
totalVolume += t.quantity;
totalNotional += t.price * t.quantity;
};
for (const auto& trade : trades) {
accumulate(trade);
}
std::cout << "Total volume = " << totalVolume << '\n';
std::cout << "Total notional = " << totalNotional << '\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