Some applications of c++--function and bind

Source: Internet
Author: User

function is a class template that overloads the operator () function call operator , so that the object of each function class is a function object.

We can use the function template like this:

#include <iostream>#include<string>#include<vector>#include<functional>using namespacestd;voidFooConst string&s) {cout<< s <<Endl;}intMainintargcConst Char*argv[]) {    void(*pfunc) (Const string&) = &foo; PFunc ("Bar"); function<void(Const string&) > F = &foo; F ("Bar"); return 0;}

So the F () function is the same as the function pointer Pfunc (), and we can call the function by passing the corresponding argument.

When using a function template, we can change the number, order, and so on by invoking the bind () function. Bind () is a function adapter.

Let's look at a simple example:

1#include <iostream>2#include <string>3#include <vector>4#include <functional>5 using namespacestd;6 7 classFoo8 {9      Public:Ten         voidFoointi) {cout << i <<Endl;}  One  A         Static voidBarDoubled) {cout << D <<Endl;} -  - }; the  - intMainintargcConst Char*argv[]) - { -     //mem_fun Void (*) (FOO *, int) + Foo F; -(Mem_fun (&foo::foo)) (&f,123);  +  Afunction<void(int) > PF = Bind (&Foo::foo, at&F, - std::p laceholders::_1); -pf345); -  -function<void(foo*,int) > PF2 = Bind (&Foo::foo, - std::p laceholders::_1, in std::p laceholders::_2); -  toPF2 (&f,456); +  -function<void(int, foo*) > PF3 = Bind (&Foo::foo, the std::p laceholders::_2, * std::p laceholders::_1); $ Panax NotoginsengPF3 (567, &f); -  the  +  A     return 0; the}

In the bind () function, we first pass the address of the function to bind, and then we determine the number and order of our parameters according to our redefined function, function<void (foo*, int) > PF2 .

void foo (int  i), function<voidint) > PF2 = Bind (&Foo::foo,                                          std::p laceholders::_1,                                          std::p laceholders::_2);

Since the Foo () function is a class member function, it has an implicit argument, so our original function contains two parameters. According to the function template, we need to pass parameters in the order of Foo *, int , in the original function,
Foo * is the first parameter, corresponding to the placement of 1, int is the second parameter, the corresponding position is 2, so we call std::p alceholders:: to determine the position of the parameter.

The placeholder _1,_2 , refers to the position of the actual function invocation arguments .

Here is a function of three parameters corresponding to the various functions possible, we can learn from it, or we can try to re-implement the following.

The code is as follows:

1#include <iostream>2#include <string>3#include <vector>4#include <functional>5 using namespacestd;6 7 voidTestintIDoubleDConst string&s)8 {9cout <<"i ="<< I <<"d ="<< D <<"s ="<< s <<Endl;Ten } One intMainintargcConst Char*argv[]) A { -function<void(int,Double,Const string&) > f1 = &test; -F1 ( A,3.14,"Foo"); the  -     //1.void (*) (int, double) -function<void(int,Double) > F2 = -Std::bind (&test, std::p laceholders::_1, std::p laceholders::_2,"Foo"); +F2 ( A,3.14); -  +     //2.void (*) (double, int, const string &) Afunction<void(Double,int,Const string&) > F3 = atStd::bind (&Test, std::p laceholders::_2, std::p laceholders::_1, std::p laceholders::_3); -F3 (3.14, A,"Foo"); -  -     //3.void (*) (const string &, int) -function<void(Const string&,int) > F4 = -Std::bind (&test, std::p laceholders::_2,3.14, std::p laceholders::_1); inF4 ("Foo", A); -  to     //4. Void (*) (const string &, int, double) +function<void(Const string&,int,Double) > f5 = -Std::bind (&Test, std::p laceholders::_2, std::p laceholders::_3, std::p laceholders::_1); theF5 ("Foo", A,3.14); *  $     //5. Void (*) (int)Panax Notoginsengfunction<void(int) > F6 = -Std::bind (&test, std::p laceholders::_1,3.14,"Foo"); theF6 ( A); +  A     //6 void (*) (const string &) thefunction<void(Const string&) > F7 = +Std::bind (&test, A,3.14, std::p laceholders::_1); -F7 ("Foo"); $  $     //7. Void (*) () -function<void() > F8 = -Std::bind (&test, A,3.14,"Foo"); the f8 (); -}
View Code

Some applications of c++--function and 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.