// bind example from http://www.cplusplus.com/reference/functional/bind/ // // It can certainly be argued that lambda-functions are nearly // a replacement for bind, but ofttimes bind expresses intention better, // and is shorter. #include // cout #include // bind double divide(double x, double y) { return x/y; } using namespace std; using namespace std::placeholders; // adds _1, _2, _3, … int main () { cout << "10÷3 = " << divide(10, 3) << '\n'; auto five = bind(divide, 10, 2); // returns 10/2 cout << "10÷2 = " << five() << '\n'; // 5 auto half = bind(divide, _1, 2); // returns x/2 cout << "12.4÷2 = " << half(12.4) << '\n'; // 6.2 auto revdiv = bind(divide, _2, _1); // returns y/x cout << "5÷4 = " << revdiv(4,5) << '\n'; // 5/4 = 1¼ auto invert = bind(divide, 1.0, _1); // returns 1/x cout << "1÷3 = " << invert(3.0) << '\n'; // ⅓ // Or, just use a lambda-function: auto invert2 = [](double d) {return divide(1.0, d);}; // returns 1/x cout << "1÷3 = " << invert2(3.0) << '\n'; // ⅓ }