-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_object_and_bind.cpp
More file actions
30 lines (24 loc) · 1.07 KB
/
function_object_and_bind.cpp
File metadata and controls
30 lines (24 loc) · 1.07 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
#include <functional>
#include <iostream>
using namespace std;
int main()
{
// plus take two arguments _1 and 10
auto plus10 = bind(plus<int>(), std::placeholders::_1, 10);
// _1 is to be 7
cout << "plus ten: " << plus10(7) << endl;
// 'auto' means compile to automaticall deduce type, you can do 'auto x = 5;' for int - but dont do it
// _1 and _2 are not needed to fully qualified by name space, hence _1 has left out, something about nameless namespace
auto inversDivide = std::bind(std::divides<double>(),
std::placeholders::_2,
std::placeholders::_1);
std::cout << "invdiv: " << inversDivide(7,49.5) << std::endl;
// Bind on bind - two FO (function objects)
auto plus10times2 = std::bind(std::multiplies<int>(),
std::bind(std::plus<int>(),
std::placeholders::_1,
10),
2);
std::cout << "+10 *2: " << plus10times2(7) << std::endl;
return 0;
}