-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathfunction_table.cc
More file actions
84 lines (73 loc) · 2 KB
/
function_table.cc
File metadata and controls
84 lines (73 loc) · 2 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
#include <iostream>
#include <functional>
#include <vector>
#include <string>
// Explicit instantiation of std::string needed, otherwise
// we get a liner error with clang on osx thats a bug in libc++
// because of interaction with
// __attribute__ ((__visibility__("hidden"), __always_inline__)) in std::string
// Without this line, &std::string::empty will be in error of 'undefined symbols'
template class std::basic_string<char>;
class Screen {
public:
using Action = Screen& (Screen::*) ();
enum Directions { HOME, FORWARD, BACK, UP, DOWN };
Screen& home() {
std::cout << "go to home\n";
return *this;
}
Screen& forward() {
std::cout << "go to forward\n";
return *this;
}
Screen& back() {
std::cout << "go to back\n";
return *this;
}
Screen& up() {
std::cout << "go to up\n";
return *this;
}
Screen& down() {
std::cout << "go to down\n";
return *this;
}
Screen& move(Directions);
private:
static Action menu[];
};
Screen& Screen::move(Directions cm)
{
return (this->*menu[cm])();
}
Screen::Action Screen::menu[] = {
&Screen::home,
&Screen::forward,
&Screen::back,
&Screen::up,
&Screen::down,
};
int main()
{
Screen myScreen;
myScreen.move(Screen::HOME);
myScreen.move(Screen::UP);
std::function<bool (const std::string&)> fcn = &std::string::empty;
std::function<bool (const std::string*)> fp = &std::string::empty;
// The callable generated by mem_fn can be called on either an object or
// a pointer:
std::vector<std::string> svec{"", "second"};
auto f = std::mem_fn(&std::string::empty);
f(*svec.begin());
if (f(&svec[0])) {
std::cout << "first element of svec is empty.\n";
}
// std::bind
using std::placeholders::_1;
auto fb = std::bind(&std::string::empty, _1);
if (fb(*svec.begin())) {
std::cout << "first element of svec is empty (bind).\n";
}
f(&svec[0]);
return 0;
}