Implementation of C++11 Closure package

Source: Internet
Author: User

Implementation of c++11 closures what is closures

Closures are defined in many ways, and one is that closures are functions with context. Plainly, it is a function of state. More directly, not just a class? It's just a different name.

A function, with a state, becomes a closed packet. What do you mean, "take a State"? This means that the closure has its own variables, the values of these variables are set when the closure is created, and when the closure is called, the variables can be accessed.

A function is code, a state is a set of variables, and a bundle of code and a set of variables (BIND) forms a closure.

The state bundle of closures must occur at run time.

Functor: overloaded operator ()
#define_crt_secure_no_warnings#include<iostream>#include<string>#include<memory>#include<vector>#include<map>classmyfunctor{ Public: Myfunctor (inttemp): Round (temp) {}int operator()(intTemp) {returnTemp +round;}Private:    intround;};voidmytest () {intRound =2;    Myfunctor f (round); Std::cout<<"Result:"<< F (1) << Std::endl;//operator () (int temp)    return;}intMain () {mytest (); System ("Pause"); return 0;}

Std::bind binding Device

In C + +, callable entities mainly include: functions, function pointers, function references, objects that can be implicitly converted to function-specified, or objects that implement Opetator ().

In C++11, a new Std::function class template is added, which is a type-safe package for existing callable entities in C + +. By specifying its template parameters, it can handle functions, function objects, function pointers in a uniform manner, and allow saving and delaying execution of them.

The greatest use of the Std::function object is to implement a function callback, and the user needs to be aware that it cannot be used to check for equality or inequality, but can be compared with null or nullptr.

#define_crt_secure_no_warnings#include<iostream>#include<string>#include<memory>#include<functional>#include<vector>#include<map>voidFuncvoid){//General global FunctionsStd::cout << __function__ <<Std::endl;}classfoo{ Public:    Static intFoo_func (inta) {//static functions in a classStd::cout << __function__ <<"("<< a <<")->:"; returnA; }};classbar{ Public:    int operator()(inta) {//Imitation functionsStd::cout << __function__ <<"("<< a <<")->:"; returnA; }};voidmytest () {//The greatest use of the Std::function object is to implement a function callback, and the user needs to be aware that it cannot be used to check for equality or inequality, but can be compared with null or nullptr. //binding a common functionstd::function<void(void) > f1 =func;    F1 (); //static functions in a binding classstd::function<int(int) > F2 =Foo::foo_func; Std::cout<< F2 ( One) <<Std::endl; //binding a functorBar obj; Std::function<int(int) > F3 =obj; Std::cout<< F3 (222) <<Std::endl; /*operation Result: Func Foo::foo_func (one)->: Bar::operator () (222)->: 222*/    return;}intMain () {mytest (); System ("Pause"); return 0;}

Std::bind

Std::bind is a mechanism that can pre-bind certain parameters of a specified callable entity to an existing variable, producing a new callable entity that is useful in the use of a callback function.

C++98, there are two functions bind1st and bind2nd, which can be used to bind the first and second parameters of functor respectively, they can only bind one parameter, various restrictions, so that bind1st and bind2nd usability greatly reduced.

In C++11, the std::bind is provided, the number of parameters it binds is unrestricted, the specific parameters of the binding are not restricted, the user specifies that bind is the real binding.

#define_crt_secure_no_warnings#include<iostream>#include<string>#include<memory>#include<functional>#include<vector>#include<map>voidFuncintXinty) {Std::cout<< x <<" "<< y <<Std::endl;}voidmytest () {Std::bind (func,1,2)(); Std::bind (func, std::p laceholders::_1,2)(1); Func (1,2); //std::p laceholders represents a placeholder//std::p laceholders::_1 is a placeholder that represents the position that will be substituted for the first parameter passed in when the function is called. Std::bind (func,2, std::p laceholders::_1) (1); Std::bind (func,2, std::p laceholders::_2) (1,2); Std::bind (func, std::p laceholders::_1, std::p laceholders::_2) (1,2); Std::bind (func, std::p laceholders::_3, std::p laceholders::_2) (1,2,3); //Std::bind (func, 2, std::p laceholders::_2) (1);//err, no second argument when called    return;}intMain () {mytest (); System ("Pause"); return 0;}

Std::bind and Std::function used together

With the use of Std::bind and std::function, all callable objects have a uniform method of operation

#define_crt_secure_no_warnings#include<iostream>#include<string>#include<memory>#include<functional>#include<vector>#include<map>classtest{ Public:    intI//non-static member variable    voidFuncintXinty) {//non-static member functionsStd::cout << x <<" "<< y <<Std::endl; }};voidmytest () {Test obj;//Creating Objects//binding non-static member functionsstd::function<void(int,int) > f1 = Std::bind (&test::func, &obj, std::p laceholders::_1, std::p laceholders::_2); F1 (1,2);//output: 1 2obj.i=Ten; //binding non-static member variablesstd::function<int& () > F2 = Std::bind (&test::i, &obj); F2 ()=123;//obj.i = 123;Std::cout <<"obj.i:"<< obj.i <<Std::endl; return;}intMain () {mytest (); System ("Pause"); return 0;}

Implementation of C++11 Closure package

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.