#include <iostream>
#include <functional>
using namespace std;
void test1(int a)
{
cout << __FUNCTION__ << endl;
}
void test(int a, int b)
{
cout << __FUNCTION__ << a <<b<<endl;
}
class Room
{
public:
static void staticFunction(int a)
{
cout << __FUNCTION__ << endl;
}
void NormalFunction(int a)
{
cout << __FUNCTION__ << endl;
}
};
class Student
{
public:
void operator()()
{
cout <<__FUNCTION__ << endl;
}
};
int main()
{
//普通函数
std::function<void(int)> f1 = test1;
f1(1);
//函数对象
std::function<void()> f5 = Student();
f5();
//适配一个参数
std::function<void(int)> f3 = std::bind(test, 1,std::placeholders::_1);
f3(9);
//调整参数顺序
std::function<void(int,int)> f4 = std::bind(test, std::placeholders::_2, std::placeholders::_1);
f4(2,1);
//类静态成员函数
std::function<void(int)> f2 = &Room::staticFunction;
f2(1);
//类普通成员函数
Room room;
std::function<void(int)> f6 = std::bind(&Room::NormalFunction, &room,std::placeholders::_1);
f6(1);
std::function<void(Room*, int)> f7 = std::bind(&Room::NormalFunction, std::placeholders::_1, std::placeholders::_2);
f7(&room, 3);
getchar();
return 0;
}