-
Notifications
You must be signed in to change notification settings - Fork 160
/
functional.cpp
96 lines (74 loc) · 2.61 KB
/
functional.cpp
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
/*
# functional
Convenient simple functions.
They implement `operator()`, which allows you to pass them to
other stdlib functions that take functions as arguments.
*/
#include "common.hpp"
int overload(int i){ return i + 1; }
int overload(int i, int j){ return i + j; }
int default_(int i, int j = 1){ return i + j; }
int main() {
// # function
{
// Disambiguate overload.
// http://stackoverflow.com/questions/12500411/setting-a-stdfunction-variable-to-refer-to-the-stdsin-function
{
std::function<int(int)> f = (int(*)(int))&overload;
assert(f(1) == 2);
std::function<int(int, int)> g = (int(*)(int, int))&overload;
assert(g(1, 2) == 3);
}
// Default vaues without bind.
// TODO possible?
{
// Fails. j seems to contain trash.
//std::function<int(int)> f = (int(*)(int))&default_;
//assert(f(1) == 2);
// Lambda workaround.
std::function<int(int)> f = [](int i){ return default_(i); };
assert(f(1) == 2);
std::function<int(int, int)> g = (int(*)(int, int))&default_;
assert(g(1, 2) == 3);
}
}
/*
# bind2nd
TODO Deprecated?
Tranform a function that takes two arguments into a function that takes only the first.
Useful with stdlib functions that must take functions that take a single argument,
but you want to pass an extra parameter to that function.
*/
{
/*
std::vector<int> v = {2, 0, 1};
assert(std::find_if (
v.begin(),
v.end(),
std::bind2nd([](int i, int param){return i == param + 1;}, 1)
) == v.begin());
*/
}
// # plus
{
assert(std::plus<int>()(1, 2) == 3);
}
#if __cplusplus >= 201103L
/*
# hash
http://www.cplusplus.com/reference/functional/hash/
The stdlib furnishes overloaded hash functions for stdlib containers.
Those functions are implemented as callable classes that implement `()`.
For base types, those hashes are found under the `functional`.
For std::vectors, only `std::vector<bool>` has a template.
For other types, they are found in the same header that defines those types:
ex: hash for std::vectors is under `<vector>`.
Returns a `size_t` result.
*/
{
std::cout << "hash" << std::endl;
std::cout << " 1 = " << std::hash<int>()(1) << std::endl;
std::cout << " string abc = " << std::hash<std::string>()("abc") << std::endl;
}
#endif
}