C++ function and bind examples
Boost documentation is good on these topics. Here is an example that I was playing with. For the purpose of discussion, ordinary (non-member) functions are called free functions, objects of type function are called functions and objects of class with overloaded operator() are functors.
/* binders.cpp ----------- Usage of function and bind. Compile with g++ --std=c++11 -obinders binders.cpp */ #include <functional> #include <iostream> #include <string> using namespace std; // free function float divide(int a, int b) { return float(a) / b; } // struct with methods struct Printer { Printer(string s) : _s(s) { } string print() { return _s; } void hello(string s) { cout << s << endl; } private: string _s; }; // struct with overloaded methods struct Logger { Logger(string s) : _s(s) { } string print() { return _s; } string print(string s) { _s = s; return _s; } private: string _s; }; // functor struct Multiplier { int operator()(int x, int y) { return x * y; } }; int main() { using namespace std::placeholders; // for _1, _2, ... Printer p("Hello, World!"); Logger l("Hello, World!"); Multiplier m; // free function to function function<float(int, int)> f_div = divide; cout << "f_div(1, 2)=" << f_div(1, 2) << endl; // member function to function function<string(Printer*)> f1_print = &Printer::print; cout << "f1_print(&p)=" << f1_print(&p) << endl; function<void(Printer*, string)> f2_print = &Printer::hello; cout << "f2_print(&p, \"Hello, World!\")="; f2_print(&p, "Hello, World!"); // member overloaded function to function function<string(Logger*)> f1_log = static_cast<string(Logger::*)()>(&Logger::print); cout << "f1_log(&l)=" << f1_log(&l) << endl; function<string(Logger*, string)> f2_log = static_cast<string(Logger::*)(string)>(&Logger::print); cout << "f2_log(&l, \"Hello, World!\")=" << f2_log(&l, "Hello, World!") << endl; // functor to function function<int(int, int)> f_multi = Multiplier(); cout << "f_multi(2, 3)=" << f_multi(2, 3) << endl; // lambda to function function<float(float, float)> fl_div = [](float x, float y) -> float { divide(x, y); }; cout << "fl_div(2, 2)=" << fl_div(2, 2) << endl; // free function binders cout << "bind(divide, 1, 2)()=" << bind(divide, 1, 2)() << endl; cout << "bind(divide, _1, 2)(3)=" << bind(divide, _1, 2)(3) << endl; cout << "bind(divide, 2, _1)(3)=" << bind(divide, 2, _1)(3) << endl; cout << "bind(divide, _2, _1)(3, 1)=" << bind(divide, _2, _1)(3, 1) << endl; cout << "bind(divide, _1, _1)(5)=" << bind(divide, _1, _1)(5) << endl; // bind free function to function function<float(int, int)> b1_div = bind(divide, _1, _2); cout << "b1_div(4, 2)=" << b1_div(4, 2) << endl; // bind free function to function; arguments copied, so new arguments do not affect the result function<float(int, int)> b2_div = bind(divide, 5, 2); cout << "b2_div(5, 2)=" << b2_div(4, 2) << endl; // functor binders cout << "bind<int>(m, _1, _2)(3, 4)=" << bind<int>(m, _1, _2)(3, 4) << endl; // bind functor to function function<int(int, int)> b_multi = bind<int>(m, _1, _2); cout << "b_multi(2, 5)=" << b_multi(2, 5) << endl; // member function binders cout << "bind(&Printer::print, ref(p))()=" << bind(&Printer::print, ref(p))() << endl; cout << "bind(&Printer::hello, ref(p), _1)(\"Hello\")="; bind(&Printer::hello, ref(p), _1)("Hello"); cout << "bind(static_cast<string(Logger::*)()>(&Logger::print), ref(l))()=" << bind(static_cast<string(Logger::*)()>(&Logger::print), ref(l))() << endl; cout << "bind(static_cast<string(Logger::*)(string)>(&Logger::print), ref(l), _1)(\"Yaba daba doo\")=" << bind(static_cast<string(Logger::*)(string)>(&Logger::print), ref(l), _1)("Yaba daba doo") << endl; return 0; }
Sample is tested under Linux and FreeBSD, gcc 4.8.1.