-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbind.cpp
More file actions
59 lines (46 loc) · 1.61 KB
/
bind.cpp
File metadata and controls
59 lines (46 loc) · 1.61 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
#include <iostream>
#include <functional>
void foo(int a, int b)
{
std::cout << "got: " << a << " " << b << std::endl;
}
void print_to(std::ostream &os, const std::string &msg)
{
os << msg << std::endl;
}
int main()
{
foo(1,2);
auto reorder_args = std::bind(foo, std::placeholders::_2, std::placeholders::_1);
/*
1. Generate a new callable named reorder_args
2. It is based on foo and
3. its first argument (_1) will be used as the second argument to foo
4. its second argument (_2) will be used as the first argument to foo
*/
reorder_args(1,2); // reordering args
auto partial_invocation = std::bind(foo, std::placeholders::_2, 5);
/*
1. Generate a new callable named partial_invocation
2. It is based on foo and
3. Its first argument is ignored
4. Its second argument is used as the first argument to foo
5. Foo is always called with the second argument being 5
*/
partial_invocation(-1,5); // -1 is ignored
auto correct_partial_invocation = std::bind(foo, std::placeholders::_1, 5);
/*
1. Generate a callable that binds foo's second argument to 5
2. Only the first argument remains free
*/
correct_partial_invocation(1);
// --- std::ref / std::cref example ---
/*
1. Some types like std::ostream are non-copyable.
2. std::bind copies arguments by default.
3. To pass such objects by reference, use std::ref (or std::cref for const ref).
*/
auto bound_printer = std::bind(print_to, std::ref(std::cout), std::placeholders::_1);
bound_printer("Hello from std::ref!");
return EXIT_SUCCESS;
}