C++11--std::function and bind bindings

Source: Internet
Author: User

Std::function and Std::bind are added in the c++11, which makes it easier to use the standard library and also allows for convenient time-delay evaluation.

Callable Object

Callable objects in C + + exist in the following categories:
(1) Function pointers
(2) Class object with operator () member function (functor)
(3) Class object that can be converted to a function pointer
(4) class member (function) pointer

void func (void) {    //...} struct foo{    void operator () (void) {        //...    }}; struct bar{    using fr_t = void (*) (void);    static void Func (void) {         //...    }    operator fr_t (void) {  //can be implicitly converted, and the result is fr_t as a function pointer        return func;     The operator xxx (void) function can implicitly convert an object of a class    }};struct a{    int a_;    void Mem_func (void) {        //...    }}; int main () {    void (* func_ptr) (void) = &func;//1. function pointer    func_ptr ();         Foo foo;                2. Imitation function    foo ();        Bar Bar;    Bar ();          Class object that can be converted to a function pointer        void (A::* mem_func_ptr) (void) = &A::mem_func;//class member function pointer        int A::* mem_obj_ptr = &a::a ; Class member pointer    return 0;}

Callable Object Wrapper--std::function

C++11 through Std::function, Std::bind unifies the various operations of callable objects. Std::function is the wrapper for a callable object, which is a class template that accommodates all callable objects except the pointer to a class member (function). By specifying its template parameters, it can handle functions, function objects, function pointers in a uniform manner, and allow for saving and delaying execution of them.
std::function<返回值(参数类型)> f;

void func (void) {}class foo{public:    static int foo_func (int a) {        std::cout << "Hello" <<std::endl;        return A;    }; int main () {    std::function<void (void) > F;    f = func;    Std::function<int (int) > = Foo::foo_func;    return 0;}

Std::bind Binding Device

    std::bind is used to bind a callable object with its arguments. The results of the binding can be saved using std::function and deferred to any time we need. It has two main functions: &NBSP;
(1) binds the callable object with its arguments as a functor &NBSP;
(2) will be multivariate (the number of parameters is n>1) callable object into an X-element (0 <= 0 <= N) can invoke the object, that is, only some parameters are bound. &NBSP;
(3) Use bind to bind non-static member functions of a class to an executable Std::function object

#include <functional>using namespace std;int func (int a, int b) {std::cout << "a =" << a << ", B = "<< b << Std::endl;return a + b;} Class A{public:int Mem_func (int A, int b) {std::cout << "a =" << a << ", B =" << b << std:: Endl;return A + b;}}; the int main () {////directly generates a callable object and then calls, with a parameter of 3//bind (func, 1, std::p laceholders::_1) that represents the parameter 1 and the parameter std::p laceholders::_ 1 is bound to another executable object as a parameter of the Func function.  At the time of binding, in order, 1 as the argument of the func (int a, int b) parameter A, std::p laceholders::_1 as the argument of the func (int a, int b) parameter B//std::p laceholders::_1 is a placeholder that represents the 1th of the argument when the executable object to be bound is called, put it to std::p laceholders::_1 position//, std::p laceholders::_2 will call the 2nd of the argument, put it in that position/ Note that the placeholder std::p laceholders::_x x must be less than or equal to the number of actual arguments at the time of the call!! Std::bind (func, 1, std::p laceholders::_1) (3);//Output A = 1, b = 3std::bind (func, 1, std::p laceholders::_2) (3, 6);//Output A = 1, b = 6//with function callable object F Save bind result Std::function<int (int) > f = Std::bind (func, 1, std::p laceholders::_1); F (2);//Output A = 1, b = 2f = Std::bind (func, StD::p laceholders::_1, 1); F (3);//Output A = 3, B = 1//the non-static member function of the class, and the object instance of the class, bind a a;f = Std::bind (&a::mem_func, A, 1, std::p LA Ceholders::_1); F (ten); return 0;}


using the Combine bind function

Bind can combine multiple functions, assuming you want to find out the number of elements in the collection that are greater than 5 and less than 10.
To determine whether a number is greater than 5 closures, the code std::bind(std::greater< int>(), std::placeholders::_1, 5)
To determine whether a number is less than 10 closures, the code std::bind(std::less_equal< int>(), std::placeholders::_1, 10)
Then combine it to get:

Using std::p laceholders::_1;//Find the number of elements in the collection that are greater than 5 and equal to 10 auto F = std::bind (Std::logic_and<bool> (), Std::bind (std:: Greater<int> (), _1, 5), Std::bind (Std::less_equal<int> (), _1, ten)); int count = Std::count_if (Coll.begin (), Coll.end (), f);

C++11--std::function and bind bindings

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.