#include <functional>
Using namespace std::p laceholders;
1. The function type can be declared in advance:function< return value (parameter list) > function name;
function< int (int, double, string) > fn
You can also directly bind the front without declaring the type: auto FN = bind (...)
corresponding to the function parameters:
The position of the declaration is corresponding to the parameters of the actual function:
int test (double, string, int)
fn = bind (test, _2, _3, _1) The first type of the-->test parameter is a double, corresponding to the _2 position of the function,
You can also set a constant parameter, FN = bind (test, 3.5, "Hello", 6);
When a member function of the 2.bind class is:
Bind (&a::p rint, &a, ...)
#include <iostream> #include <string> #include <functional>using namespace std;using namespace std:: placeholders;void Test (int i, double D, const string &s) {cout << "i=" << I << "d=" << D << "s=" << s << endl;} int test1 (int i, double D, const string &s) {cout << "i=" << I << "d=" << D << "s= "<< s << endl; return i;} int main (int argc, const char *argv[]) {function<void (void) > fp; string s = "Foo"; int a = 3; Double b = 6.7; fp = bind (&test, A, B, s); FP (); function <void (int, const string&) > FP1; Double b2 = 4.6; FP1 = bind (test, _1, B2, _2); FP1 (4, "kity"); function <int (int, const string&) > FP2; FP2 = bind<int> (Test1, _1, B2, _2); int y = FP2 (4, "kity"); cout << y << Endl; AUTO FN = bind (test, ten, 23.3, "Heko");//function parameter not specified, FN (); Auto FF = bind (tesT, _1, _3, _2); the I parameter type in//bind corresponds to the parameter type of the _j of Test FF ("PPP", 12.5); Class A {public:void print (int a, double x) {cout << A << " "<< x << Endl; } }; A A3; Auto fclass= bind (&a::p rint, &A3, 11, 7.7); Fclass (); return 0;}
function bind callback mechanism of C++11