C++11function

Source: Internet
Author: User

A few new templates have been added to the c++11, and today we have a brief introduction to function templates.

Header file:<functional>

function is a template, and when you create a specific function type, we have to provide additional information like other class templates. The so-called extra information refers to the invocation form of the object that the function type can represent. For example, vector, we must specify the type of data that is stored in the vector, whether it is int, string, or double. We must specify clearly.

Functions are similar to function pointers, and the advantage is that it allows the user to have greater flexibility in the implementation of the target, that is, the target can be either a normal function or a member function of a function object and a class.

First, the normal function:

First, we give you a normal function:

void foo (conststring &s) {    << s << Endl;}

So we can use function in the main function.

function<void(conststring &s) > F =&foo;f ("World ");

The test results are: world. The above function can be interpreted as:

Declares and initializes a function that returns a type of void, accepts a const string parameter, and causes F to be initialized to the address of the Foo function.


Second, class member functions:

We define a class with the following code:

class foo{    public:        void Foo (int  a)        {            << a << Endl;        }};

We used one of the following calling methods:

// header file #include <iostream><string><functional>using namespace std;
//Main functionintMainintargcConst Char*argv[]) {Foo F;//1(Mem_fun (&foo::foo) (&f,123));//2function<void(int) > PF =Bind (&Foo::foo,&F, placeholders::_1); PF (345);//3function<void(foo*,int) > Pf2 =bind (&Foo::foo, std::p laceholders::_1, std::p laceholders::_2); PF2 (&f,345); return 0;}

Mode one: Mem_fun, see pointers to member functions of C + + classes and usage of Mem_fun adapters
Way two:

function<void(int) > PF =bind (&foo::foo, &f, placeholders::_1);

Attention:

First, bind is a function adapter that can change the number of parameters , in order .
Second, the arguments in bind are, in turn, the address of the member function of Class Foo, the address of an object F of Class Foo, and the placeholder for the parameter int.

The placeholder _1, _2, refers to the position of the actual function invocation arguments. Also, placeholders must be consecutive, non-jumping, and the argument list in bind can be _1,_2,_3 .... It can also be _1,_3,_2 (that is, the order can be scrambled), but it must not be such a bind (&foo::foo, _1, _3)

Way three:

function<voidint) > Pf2 =bind (&foo::foo,std::p laceholders::_1, std::p laceholders::_2);

Because the member function of the class has an implicit parameter of this. So, there are two actual arguments to the Foo function.
In the third way, the implicit parameter of this is indicated, so this corresponds to the placeholder in bind _1,int corresponding to the placeholder _2.

Third, examples:

#include <iostream>#include<string>#include<functional>using namespacestd;using namespacestd::p laceholders;voidTestintIDoubleDConst string&s) {cout<<"i="<< I <<"d="<< D <<"s="<< s <<Endl;}intMainintargcConst Char*argv[]) {function<void(int,Double,Const string&) >f1 = &test; F1 (1,3.14,"Foo"); //1//Void (*) (int, double)function<void(Double,int,Const string&s) > F2 = Bind (&test, _2, _1, _3); F2 (4.14,2,"Hello");//2function<void(int,Double) > F3= bind (&test, _1, _2," World"); F3 (3,5.14);//3function<void(Const string&,int) > F4 = Bind (&test, _2,6.14, _1); F4 (" How",4);//4function<void(Const string&,int,Double) > f5 = Bind (&test, _2, _3, _1); F5 (" is",5,7.14);//5function<void(int) > F6 = Bind (&test, _1,8.14," You"); F6 (6);//6function<void(Const string&) > F7 =bind (&test,7,9.14, _1); F7 ("Thank");//7function<void() > F8 = Bind (&test,8,10.14,"Foobar");    F8 (); return 0;}

C++11function

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.