C + + one bind function

Source: Internet
Author: User

Year Chen Liangjio c++11 FAQ

Std::function and Std::bind

The standard library functions bind () and function () are defined in the header file (which also includes many other function objects) for handling functions and function parameters. Bind () accepts a function (or function object, or anything you can pass through "(...)" Symbols called), which generates a function object that has one or more function arguments that are "bound" or re-organized. As the name implies, the meaning of the bind () function is like its function name, which is used to bind certain parameters of a function call. For example

        int f (int, char, double);        Bind the second and third arguments of the F () function call,        //Return a new function object to FF, which only has an int type parameter        auto FF = bind (f, _1, ' C ', 1.2);        int x = FF (7);  F (7, ' C ', 1.2);

The binding of parameters is often referred to as "currying" (currying-"Cooking curry Cooking", which means processing the function or function object), "_1″ is a placeholder object that indicates when function f is called through the function ff. The position of the first parameter of the function ff in the argument list of the function F. The first parameter is called "_1″, the second argument is" _2″, and so on. For example:

        int f (int, char, double);        Auto Frev = bind (f, _3, _2, _1);        Flip parameter order        int x = Frev (1.2, ' C ', 7);            F (7, ' C ', 1.2);

Here, the Auto keyword saves us the effort to infer the type of result that bind returns.

We cannot bind a parameter of an overloaded function using bind (), and we must explicitly indicate the version of the overloaded function that needs to be bound :

        int g (int);        Double g (double);        Auto G1 = Bind (g, _1);    Error: which g () is called?        Correct, but rather ugly        auto g2 = Bind ((double (*) (double)) g, _1);

There are two versions of BIND (): One as described above, the other a "legacy" version: You can explicitly describe the return type. For example:

        Auto F2 = bind<int> (f, 7, ' C ', _1);      An explicit return type        int x = F2 (1.2);                    F (7, ' C ', 1.2);

The existence of the second form is necessary, and since the first version (?) "And for a user simplest", please refer to the original text here)) cannot be implemented in c++98. So the second version has been widely used.

function is a owning anything that can be "(...)" The type of the value called by the symbol. In particular, the return result of bind can be assigned to the function type. function is very easy to use. More intuitively, you can think of functions as a data type that represents a function, just like a function object. Just plain data types represent data, and function represents the abstract concept of functions. For example

        Constructs a function object,        //It can represent a return value of float,        //two parameters of Int,int function        function<float (int x, int y) > F;           Constructs a function object type that can be called using "()"        struct Int_div {                float operator () (int x, int y) const                     {return ((float) x)/y;};        };        f = Int_div ();                    Assignment        cout<< F (5,3) <<endl;  Call        Std::accumulate (b, E, 1, f) through a function object;        Perfect delivery

member functions can be considered as free functions with additional parameters:

        struct X {                int foo (int);        };        The so-called extra parameter,        //is the default first parameter of the member function,//That is, the this pointer to the        object calling the member function        function<int (x*, int) > F;        f = &X::foo;            Point to member function        x x;        int v = f (&x, 5);  Call X::foo ()        function<int (int) > FF = Std::bind (f, &x, _1) on object x with parameter 5;    The first parameter of F is &x        v = FF (5);                Call X.foo (5)

function is useful for callback functions, passing operations as parameters, and so on. It can be seen as a substitute for function object mem_fun_t, pointer_to_unary_function, etc. in the C++98 standard library. Similarly, bind () can also be seen as a substitute for bind1st () and bind2nd (), and certainly more powerful and flexible than they are.

Reference:

    • standard:20.7.12 function template bind, 20.7.16.2 Class template Function
    • Herb sutter:generalized Function Pointers
      .
      August 2003.
    • Douglas Gregor:
      Boost.function
      .
    • Boost::bind

(translation: Dabaitu)

C + + one bind function

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.