#include#include using namespace std::placeholders; // for _1 void print_sum(int a, int b, int c) { std::cout << a + b + c << '\n'; } int main() { auto bind_fun = std::bind(print_sum, 1, 2, _1); bind_fun(3); // prints 6 }
#includeIn this example, we first create an object of class A called "a_obj". We then use std::bind to create a new function object called "bind_fun", which is created by binding the "print" method of class A with the object "a_obj", and using std::placeholders::_1 to pass the parameter. Here, we are calling the "bind_fun" function by passing the argument 10, which then calls the "print" method of class A with the object "a_obj" and the argument 10, and prints the output. The package/library used in the above examples is the C++ Standard Library.#include class A { public: void print(int x) { std::cout << "A::print(" << x << ")" << '\n'; } }; int main() { A a_obj; auto bind_fun = std::bind(&A::print, &a_obj, _1); bind_fun(10); // prints A::print(10) }