-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbind.cpp
More file actions
executable file
·117 lines (94 loc) · 3.73 KB
/
bind.cpp
File metadata and controls
executable file
·117 lines (94 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <algorithm>
#include <cmath>
#include <functional> // std::bind, std::placeholders, std::function
#include <iostream>
#include <vector>
/*
std::bind is a functional adaptor: it takes a callable and returns a new
callable with some arguments pre-bound or reordered (partial application).
Real-world feel: bind a sensor-read function so a scheduler can call it with
no extra context, or bind a member function (e.g. Motor::set_torque) onto a
specific instance so it can be stored in a std::function<void(double)>.
See docs/callable_callbacks.md, sections 3 and 5.
*/
// A free function we will partially apply with std::bind.
void log_event(int actuator_id, int code) {
std::cout << "[actuator " << actuator_id << "] event code: " << code << '\n';
}
// A function template to show that bind also works with instantiated
// templates.
template <typename T> void print_template(T value) {
std::cout << value << '\n';
}
// A class with a member function — bind is the classic way to turn
// &Class::method into a free-standing callable bound to a specific instance.
class Motor {
public:
explicit Motor(int id) : id_(id) {}
void set_torque(double nm) const {
std::cout << "Motor " << id_ << " torque -> " << nm << " Nm\n";
}
private:
int id_;
};
int main() {
// 1. Bind by value vs std::ref: captured-by-value snapshots arguments at
// bind time; std::ref keeps a live reference, so later mutations show
// up when the bound callable is invoked.
{
std::cout << "--- bind by value vs std::ref ---\n";
int actuator_id = 3;
int code = 5;
auto by_value = std::bind(&log_event, actuator_id, code);
auto by_ref = std::bind(&log_event, std::ref(actuator_id), std::ref(code));
actuator_id = 4;
code = 6;
by_value(); // prints 3, 5 (snapshot)
by_ref(); // prints 4, 6 (live reference)
}
// 2. Reorder/forward arguments with std::placeholders.
{
std::cout << "--- placeholders reorder arguments ---\n";
auto reversed =
std::bind(&log_event, std::placeholders::_2, std::placeholders::_1);
log_event(1, 99); // original order
reversed(1, 99); // swapped: actuator 99, code 1
}
// 3. Wrap a bind expression in std::function for a stable, type-erased
// callable type that can be stored, copied, and passed around.
{
std::cout << "--- std::function holding a bind expression ---\n";
auto bound = std::bind(&log_event, std::placeholders::_1, 42);
std::function<void(int)> emit_with_fixed_code = bound;
emit_with_fixed_code(7); // actuator 7, code 42
}
// 4. Bind to an instantiation of a function template.
{
std::cout << "--- bind to a template instantiation ---\n";
auto printer = std::bind(&print_template<int>, std::placeholders::_1);
std::function<void(int)> f(printer);
f(123);
}
// 5. Use a bind expression as the projection in <algorithm>: cube every
// sample by partially applying std::pow with the exponent fixed.
{
std::cout << "--- bind in std::transform ---\n";
std::vector<int> samples = {1, 2, 3};
std::vector<int> cubed(samples.size(), 0);
auto cube = std::bind(&std::pow<int, int>, std::placeholders::_1, 3);
std::transform(samples.begin(), samples.end(), cubed.begin(), cube);
for (auto v : cubed)
std::cout << v << '\n';
}
// 6. Bind a member function onto an instance — the canonical "wrap a
// method as a callback" pattern. The first bound argument is the
// object (or pointer); placeholders take the remaining arguments.
{
std::cout << "--- bind a member function ---\n";
Motor m{17};
auto set_torque_on_m =
std::bind(&Motor::set_torque, &m, std::placeholders::_1);
std::function<void(double)> torque_cb = set_torque_on_m;
torque_cb(12.5);
}
}