一、std..function
#include <iostream>
#include <future>
#include <vector>
#include <map>
#include <functional>
#include <string>
#include <algorithm>
#include <ctime>
#include <initializer_list>
#include <stdarg.h>
#include <list>
using namespace std;
class CallBack
{
std::function<void()> fcallback;
public:
CallBack(const std::function<void()>& f) :fcallback(f)
{
cout << "带参数的构造函数 CallBack(const std::function<void()>& f) :fcallback(f) 执行了." << endl;
}
void runCallBack(void)
{
fcallback();
}
};
class CT
{
public:
void operator()(void)
{
cout << "void operator()(void) 执行了" << endl;
}
};
int main()
{
CT ct;
CallBack cb(ct);
cb.runCallBack();
return 0;
}
#include <iostream>
#include <future>
#include <vector>
#include <map>
#include <functional>
#include <string>
#include <algorithm>
#include <ctime>
#include <initializer_list>
#include <stdarg.h>
#include <list>
using namespace std;
void mycallback(int cs, const std::function<void(int)>& f)
{
f(cs);
}
void runfunc(int x)
{
cout << x << endl;
}
int main()
{
for (int i = 0; i < 10; i++)
{
mycallback(i,runfunc);
}
return 0;
}
二、std..bind
1.std::bind 是C++11 引入的类模板,能够将对象以及相关的参数绑定到一起,绑定完之后可以直接调用,也可以用std::function 进行保存,再需要的时候进行调用。、
格式:std::bind(呆绑定的函数对象/函数指针/成员函数指针/,参数绑定值1,参数绑定值2,...,参数绑定值n)
总结:
(a):将可调用对象和参数绑定到一起,构成一个仿函数,所以可以直接调用。
(b):如果函数有多个参数,可以绑定一部分参数,其他参数在调用的时候指定。
#include <iostream>
#include <future>
#include <vector>
#include <map>
#include <functional>
#include <string>
#include <algorithm>
#include <ctime>
#include <initializer_list>
#include <stdarg.h>
#include <list>
using namespace std;
void myfunc1(int x, int y, int z=30)
{
cout << "x = " << x << ",y = " << y << ",z = " << z << endl;
}
void myfunc2(int& x, int& y)
{
x++;
y++;
}
class CT
{
public:
void myfuncpt(int x, int y)
{
cout << "x=" << x << ",y=" << y << endl;
m_a = x;
}
int m_a = 0;
};
int main()
{
auto bf1 = std::bind(myfunc1,10,20,40);
bf1();
auto bf2 = std::bind(myfunc1,placeholders::_1,20,30);
bf2(100); //bind中有占位符,此时必须显示指定参数
std::bind(myfunc1, placeholders::_1, 20, 30)(100); //bf2(100);与这样的写法等价。
auto bf3 = std::bind(myfunc1, placeholders::_2, placeholders::_1, 30);
bf3(5,15); //15,5,30
int a = 2;
int b = 3;
auto bf4 = std::bind(myfunc2, a, placeholders::_1);
bf4(b);
cout << "a=" << a << ",b=" << b << endl;
//a=2,b=4
//1.与我们想象的不一样,我们认为是 a=3,b=4。
//2.这说明:bind对于预先绑定的函数参数是通过值传递的方式进行传递的,所以这个时候a=2,不是为3。
//3.bind对于不事先绑定的参数,通过std::placeholder传递的参数,是通过引用传递的,所以b为4。
CT ct;
auto bf5 = std::bind(&CT::myfuncpt,ct,placeholders::_1,placeholders::_2);
bf5(1,2);
cout << "ct.m_a = " << ct.m_a << endl; //为什么这里的m_a是 0??
//因为:
//1.auto bf5 = std::bind(&CT::myfuncpt,ct,placeholders::_1,placeholders::_2); 这一行代码会导致调用CT的拷贝构造函数来生成一个 CT类型的临时对象,
//来作为std::bind的返回值(bind返回仿函数类型对象)。
//2.所以后续的 myfuncpt 调用修改的是这个临时对象的m_a值,并不是真实m_a的值!!!
//采用引用,这会修改真实的m_a的值!就不生成了临时的CT对象了,此时bind返回的对象是真实的CT对象本身。
auto bf6 = std::bind(&CT::myfuncpt, &ct, placeholders::_1, placeholders::_2);
bf6(1, 2);
cout << "ct.m_a = " << ct.m_a << endl; //为什么这里的m_a是 0??
return 0;
}
std::bind 和 std::function 的配合使用
#include <iostream>
#include <future>
#include <vector>
#include <map>
#include <functional>
#include <string>
#include <algorithm>
#include <ctime>
#include <initializer_list>
#include <stdarg.h>
#include <list>
using namespace std;
void myfunc1(int x, int y, int z=30)
{
cout << "x = " << x << ",y = " << y << ",z = " << z << endl;
}
void myfunc2(int& x, int& y)
{
x++;
y++;
}
class CT
{
public:
CT()
{
cout << "CT() 构造函数执行" << endl;
}
CT(const CT& ct)
{
cout << "CT(const CT& ct) 拷贝构造函数执行" << endl;
}
~CT()
{
cout << "~CT() 析构函数执行" << endl;
}
void myfuncpt(int x, int y)
{
cout << "x=" << x << ",y=" << y << endl;
m_a = x;
}
int m_a = 0;
};
int main()
{
/*
auto bf1 = std::bind(myfunc1,10,20,40);
bf1();
auto bf2 = std::bind(myfunc1,placeholders::_1,20,30);
bf2(100); //bind中有占位符,此时必须显示指定参数
std::bind(myfunc1, placeholders::_1, 20, 30)(100); //bf2(100);与这样的写法等价。
auto bf3 = std::bind(myfunc1, placeholders::_2, placeholders::_1, 30);
bf3(5,15); //15,5,30
int a = 2;
int b = 3;
auto bf4 = std::bind(myfunc2, a, placeholders::_1);
bf4(b);
cout << "a=" << a << ",b=" << b << endl;
//a=2,b=4
//1.与我们想象的不一样,我们认为是 a=3,b=4。
//2.这说明:bind对于预先绑定的函数参数是通过值传递的方式进行传递的,所以这个时候a=2,不是为3。
//3.bind对于不事先绑定的参数,通过std::placeholder传递的参数,是通过引用传递的,所以b为4。
CT ct;
auto bf5 = std::bind(&CT::myfuncpt,ct,placeholders::_1,placeholders::_2);
bf5(1,2);
cout << "ct.m_a = " << ct.m_a << endl; //为什么这里的m_a是 0??
//因为:
//1.auto bf5 = std::bind(&CT::myfuncpt,ct,placeholders::_1,placeholders::_2); 这一行代码会导致调用CT的拷贝构造函数来生成一个 CT类型的临时对象,
//来作为std::bind的返回值(bind返回仿函数类型对象)。
//2.所以后续的 myfuncpt 调用修改的是这个临时对象的m_a值,并不是真实m_a的值!!!
//采用引用,这会修改真实的m_a的值!就不生成了临时的CT对象了,此时bind返回的对象是真实的CT对象本身。
auto bf6 = std::bind(&CT::myfuncpt, &ct, placeholders::_1, placeholders::_2);
bf6(1, 2);
cout << "ct.m_a = " << ct.m_a << endl; //为什么这里的m_a是 0??
//std::bind 和 std::function 的配合使用
std::function<void(int,int)> bf7 = std::bind(&CT::myfuncpt, ct, placeholders::_1, placeholders::_2);
bf7(11, 22);
*/
//把成员变量地址当函数一样绑定,绑定的结果放在std::function<int &(void)>里保存;就是 一个可调用对象保存成员变量。
CT ct;
std::function<int& ()> bf8 = std::bind(&CT::m_a,ct); //如果ct没有用引用(&),那么m_a 肯定就是0.并且会多执行2次拷贝构造函数
bf8() = 80;
cout << "ct.m_a = " << ct.m_a << endl;
return 0;
}
#include <iostream>
#include <future>
#include <vector>
#include <map>
#include <functional>
#include <string>
#include <algorithm>
#include <ctime>
#include <initializer_list>
#include <stdarg.h>
#include <list>
using namespace std;
void myfunc1(int x, int y, int z=30)
{
cout << "x = " << x << ",y = " << y << ",z = " << z << endl;
}
void myfunc2(int& x, int& y)
{
x++;
y++;
}
class CT
{
public:
CT()
{
cout << "CT() 构造函数执行" << endl;
}
CT(const CT& ct)
{
cout << "CT(const CT& ct) 拷贝构造函数执行" << endl;
}
~CT()
{
cout << "~CT() 析构函数执行" << endl;
}
/*void myfuncpt(int x, int y)
{
cout << "x=" << x << ",y=" << y << endl;
m_a = x;
}
int m_a = 0;*/
public:
void operator()()
{
cout << "void operator()() 被调用了:"<<this << endl;
}
};
int main()
{
/*
auto bf1 = std::bind(myfunc1,10,20,40);
bf1();
auto bf2 = std::bind(myfunc1,placeholders::_1,20,30);
bf2(100); //bind中有占位符,此时必须显示指定参数
std::bind(myfunc1, placeholders::_1, 20, 30)(100); //bf2(100);与这样的写法等价。
auto bf3 = std::bind(myfunc1, placeholders::_2, placeholders::_1, 30);
bf3(5,15); //15,5,30
int a = 2;
int b = 3;
auto bf4 = std::bind(myfunc2, a, placeholders::_1);
bf4(b);
cout << "a=" << a << ",b=" << b << endl;
//a=2,b=4
//1.与我们想象的不一样,我们认为是 a=3,b=4。
//2.这说明:bind对于预先绑定的函数参数是通过值传递的方式进行传递的,所以这个时候a=2,不是为3。
//3.bind对于不事先绑定的参数,通过std::placeholder传递的参数,是通过引用传递的,所以b为4。
CT ct;
auto bf5 = std::bind(&CT::myfuncpt,ct,placeholders::_1,placeholders::_2);
bf5(1,2);
cout << "ct.m_a = " << ct.m_a << endl; //为什么这里的m_a是 0??
//因为:
//1.auto bf5 = std::bind(&CT::myfuncpt,ct,placeholders::_1,placeholders::_2); 这一行代码会导致调用CT的拷贝构造函数来生成一个 CT类型的临时对象,
//来作为std::bind的返回值(bind返回仿函数类型对象)。
//2.所以后续的 myfuncpt 调用修改的是这个临时对象的m_a值,并不是真实m_a的值!!!
//采用引用,这会修改真实的m_a的值!就不生成了临时的CT对象了,此时bind返回的对象是真实的CT对象本身。
auto bf6 = std::bind(&CT::myfuncpt, &ct, placeholders::_1, placeholders::_2);
bf6(1, 2);
cout << "ct.m_a = " << ct.m_a << endl; //为什么这里的m_a是 0??
//std::bind 和 std::function 的配合使用
std::function<void(int,int)> bf7 = std::bind(&CT::myfuncpt, ct, placeholders::_1, placeholders::_2);
bf7(11, 22);
//把成员变量地址当函数一样绑定,绑定的结果放在std::function<int &(void)>里保存;就是 一个可调用对象保存成员变量。
CT ct;
std::function<int& ()> bf8 = std::bind(&CT::m_a,ct); //如果ct没有用引用(&),那么m_a 肯定就是0.
bf8() = 80;
cout << "ct.m_a = " << ct.m_a << endl;
*/
CT ct;
auto rt = std::bind(CT()); //这样会多执行一次拷贝构造函数
rt();
return 0;
}
#include <iostream>
#include <future>
#include <vector>
#include <map>
#include <functional>
#include <string>
#include <algorithm>
#include <ctime>
#include <initializer_list>
#include <stdarg.h>
#include <list>
using namespace std;
void mycallback(int cs, const std::function<void(int)>& f)
{
f(cs);
}
void runfunc(int x)
{
cout << x << endl;
}
int main()
{
auto bf = std::bind(runfunc,std::placeholders::_1);
for (int i = 0; i < 10; i++)
{
mycallback(i,bf);
}
return 0;
}
总结:
1.bind的思想 所谓的延迟调用,将可调用的对象统一格式,保存起来;需要的时候在调用。
2.有std::function 绑定一个可调用对象,类型成员不能绑定。std::bind 成员函数,成员变量等等都能绑定。