The use of C++11-bind

Source: Internet
Author: User

Bind function

Prior to c++11, it was necessary to bind a function, function object, or member function to different parameter values, such as bind1st, bind2nd, Fun_ptr, Mem_fun, and Mem_fun_ref. In C++11, The method of binding parameters is simplified. C++11 provides a "one-stop" binding template bind, which is used as:

#include <functional> Std::bind ( function object/function pointer/member function pointer to bind, parameter binding value 1, parameter binding value 2,..., parameter binding value n);

The first parameter of BIND is the function object or function pointer to be bound, followed by several parameters to set the parameter binding method of the function to be bound. To bind a function with the number of arguments, then bind will require how many parameters to declare the binding method of its arguments with one by one. When a parameter is bound to a fixed value, its corresponding parameter binding value can make a variable or constant. When you need to associate a parameter with a parameter of a function object that is generated by the binding, You need to use several constants that are predefined in the standard _1, _2, _3, and so on. These constants are declared within the std::p laceholders namespace.

Note points for binding member functions:

In the will an R (T::* ptr) (Arg0,arg1,...) In the form of a member function pointer to PTR with bind binding parameter, the first bound parameter of BIND is the caller of the member function, followed by the parameter binding method of the member function. For example, bind (PTR,A,B,C) will call A.*ptr (B,C). When a _n constant is used to associate the first parameter with the parameters of a function object, the resulting function object automatically accepts the type of reference and pointer of type T, and no further encapsulation is required. But to invoke the member function of the external data, you also need to wrap or bind a pointer to the variable with ref (), CREF ().

1#include <functional>2#include <iostream>3 4 //a custom integer class5 structinteger{6       inti;7Integerinti): I (i) {}8       voidIncr_by (intj) {i+=J;}9 };Ten  One voidmem_func_sample () { AInteger x (0);//x.i=0; -Integer *y=&x;//y point to x -      using namespacestd::p laceholders; the      using namespacestd; -       -Auto F0=bind (&integer::incr_by,_1,_2); -F0 (x,2);//x.incr_by (2) +cout<<x.i<<Endl; -       +F0 (Y,2);//y->incr_by (2) Acout<<x.i<<endl;//x.i=4 at       -Auto F1=bind (&integer::incr_by,x,_1); -F1 (2);//x A copy of the F1. INCR (2) -cout<<x.i<<endl; X.i=4; -       -Auto F2=bind (&integer::ince_by,ref(x), _1); inF2 (2);//x.incr_by (2) -cout<<x.i<<endl;//x.i=6; to       +Auto F3=bind (&integer::incr_by,&x,_1); -F3 (2);//(&x)->incr_by (2) thecout<<x.i<<endl;//x.i=8 *       $      //using MEM_FN to convert member functions to function ObjectsPanax NotoginsengAuto F4=mem_fn (&integer::incr_by); -F4 (X,2);  thecout<<x.i<<endl;//x.i=10; +F4 (Y,2);  Acout<<x.i<<endl;//x.i=12 the       +       -}

In the previous example, the parameters for the Incr_by member function of the integer class are bound in different ways. For the resulting function object F0, because the first parameter of BIND is associated with the first parameter of F0, F0 automatically takes the integer as the parameter type, so F0 (x,2) actually calls X.INCR _by (2) increases the I value of X by 2, and the F0 automatically supports Integr pointers, so calling F0 (y,2) equals y->incr_by (2), which also increases the I value of X by 2. The F1 call is a copy of x in F1 and does not affect the state of X. Only the member function of x is actually called if X is bound as F2 or F3.

The use of C++11-bind

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.