Lambdas and Callables
Function Callbacks
std::function can store a callable and pass it to another function.
callback
A callback is a callable passed to another function so that function can call it later.
Function Callbacks
function_callbacks.cpp
Replay: real traced execution (multi-file project)
#include <functional>
#include <iostream>
void repeat(int count, const std::function<void(int)>& callback) {
for (int index = 1; index <= count; index++) {
callback(index);
}
}
int main() {
int count = 3;
int total = 0;
repeat(count, [&total](int index) {
total += index;
});
std::cout << "total=" << total << std::endl;
std::cout << "done=" << count << std::endl;
return 0;
}
#include <functional>
#include <iostream>
void repeat(int count, const std::function<void(int)>& callback) {
for (int index = 1; index <= count; index++) {
callback(index);
}
}
int main() {
int count = 1;
int total = 0;
repeat(count, [&total](int index) {
total += index;
});
std::cout << "total=" << total << std::endl;
std::cout << "done=" << count << std::endl;
return 0;
}
#include <functional>
#include <iostream>
void repeat(int count, const std::function<void(int)>& callback) {
for (int index = 1; index <= count; index++) {
callback(index);
}
}
int main() {
int count = 4;
int total = 0;
repeat(count, [&total](int index) {
total += index;
});
std::cout << "total=" << total << std::endl;
std::cout << "done=" << count << std::endl;
return 0;
}
count ← 3, total ← 0
10int main() {11 int count→ 3 = 3; //@count=1, 412 int total→ 0 = 0;1314 repeat(count3, [&total](int index) {15 total += index;16 });void repeat(int count, const std::function<void(int)>& callback)
4void repeat(int count3, const std::function<void(int)>& callback(empty)) {5 for (int index = 1; index <= count; index++) {for (int index = 1; index <= count; index++)
pass 1 of 34void repeat(int count, const std::function<void(int)>& callback) {5 for (int index1 = 1; index <= count3; index++) {6 callback(empty)(index1);7 }All 3 passes — pass 1 is the card above pass index1 1 2 2 3 3 repeat(count, [&total](int index)
14 repeat(count3, [&total](int index) {15 total += index;16 });1718 std::cout << "total=" << total6 << std::endl;19 std::cout << "done=" << count3 << std::endl;20 return 0;21}outputtotal=6 done=3
count ← 1, total ← 0
10int main() {11 int count→ 1 = 1;12 int total→ 0 = 0;1314 repeat(count1, [&total](int index) {15 total += index;16 });void repeat(int count, const std::function<void(int)>& callback)
4void repeat(int count1, const std::function<void(int)>& callback(empty)) {5 for (int index = 1; index <= count; index++) {for (int index = 1; index <= count; index++)
4void repeat(int count, const std::function<void(int)>& callback) {5 for (int index1 = 1; index <= count1; index++) {6 callback(empty)(index1);7 }repeat(count, [&total](int index)
14 repeat(count1, [&total](int index) {15 total += index;16 });1718 std::cout << "total=" << total1 << std::endl;19 std::cout << "done=" << count1 << std::endl;20 return 0;21}outputtotal=1 done=1
count ← 4, total ← 0
10int main() {11 int count→ 4 = 4;12 int total→ 0 = 0;1314 repeat(count4, [&total](int index) {15 total += index;16 });void repeat(int count, const std::function<void(int)>& callback)
4void repeat(int count4, const std::function<void(int)>& callback(empty)) {5 for (int index = 1; index <= count; index++) {for (int index = 1; index <= count; index++)
pass 1 of 44void repeat(int count, const std::function<void(int)>& callback) {5 for (int index1 = 1; index <= count4; index++) {6 callback(empty)(index1);7 }All 4 passes — pass 1 is the card above pass index1 1 2 2 3 3 4 4 repeat(count, [&total](int index)
14 repeat(count4, [&total](int index) {15 total += index;16 });1718 std::cout << "total=" << total10 << std::endl;19 std::cout << "done=" << count4 << std::endl;20 return 0;21}outputtotal=10 done=4