/
functional2.cpp
35 lines (29 loc) · 1.08 KB
/
functional2.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
#include <iostream>
#include <functional>
template <typename T>
class CAnyData {
public:
T m_value;
CAnyData(T value) : m_value{value} {}
void print(void) { std::cout << m_value << std::endl; }
void printAfterAdd(T value) { std::cout << (m_value + value) << std::endl; }
};
int main(void) {
/* A function wrapper to a member variable of a class */
CAnyData<int> dataA{2016};
std::function<int(CAnyData<int>&)> funcA = &CAnyData<int>::m_value;
std::cout << funcA(dataA) << std::endl;
/* A function wrappter to member function without parameter passing */
CAnyData<float> dataB{2016.1};
std::function<void(CAnyData<float>&)> funcB = &CAnyData<float>::print;
funcB(dataB);
/* A function wrappter to member function with passing a parameter */
std::function<void(CAnyData<float>&, float)> funcC =
&CAnyData<float>::printAfterAdd;
funcC(dataB, 0.1);
/* A function wrappter to member function generated by std::bind */
std::function<void(float)> funcD =
std::bind(&CAnyData<float>::printAfterAdd, &dataB, std::placeholders::_1);
funcD(0.2);
return 0;
}