C++11 Std::bind           

C++ 11 std::bind

参考 bind 用来绑定函数调用的某些参数.

format

std::bind(函数名,参数,…)

#include <iostream>
#include <functional>
using namespace std;

int TestFunc(int a, char c, float f)
{
    cout << a << endl;
    cout << c << endl;
    cout << f << endl;

    return a;
}
auto bindFunc1 = bind(TestFunc, std::placeholders::_1, 'A', 100.1);

std::bind 返回类型

std::bind 的返回类型是一个 std::function<返回类型(函数名,参数类型)>,参数类型是各个 std::placeholders 的类型,函数名如果已经 bind 就不再出现在<> 中。比如

std::function<void(WorkflowObserver*)> func =
std::bind(&WorkflowObserver::OnPreviewStarted, std::placeholders::_1,
    acqStartData,
    PreviewStepPtr(new PreviewStepImpl(m_ManagerImpl)),
    ResultStackPtr());          上面的代码中 <> 中,

修改上面代码, 用 std::placeholders::_2 代替 ResultStackPtr()

std::function<void(WorkflowObserver*, ResultStackPtr)> func =
std::bind(&WorkflowObserver::OnPreviewStarted, std::placeholders::_1,
	acqStartData,
	PreviewStepPtr(new PreviewStepImpl(m_ManagerImpl)),
	std::placeholders::_2);            则 <> 中第二个参数类型 ResultStackPtr,就是 std::placeholders::\_2 的类型