#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 _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;}
Reference 39828207
C++11 placeholder placeholders and function bind usage