Original address: http://www.jellythink.com/archives/771
Look at this piece of code.
Let's take a look at the following two lines of code:
std::function<void(Eventkeyboard::keycode, event*) > onkeypressed;std::function< void(Eventkeyboard::keycode, event*) > onkeyreleased;
These two lines of code are extracted from Cocos2d-x, with the emphasis on the definition of these two lines of code. std::function
What is this thing? If you have no pressure on the above two lines of code, then you might as well look at this article, so be aware of the new.
Std::function Introduction
The class template std::function
is a general-purpose, polymorphic function encapsulation. std::function
instance can store, copy, and invoke operations on any target entity that can be called, including normal functions, lambda expressions, function pointers, and other function objects. An std::function
object is a type-safe package for an existing callable entity in C + + (we know that a callable entity such as a function pointer is not a safe type).
Usually Std::function is a function object class that wraps any other function object, the wrapped function object has n parameters of type T1, ..., TN, and returns a value that can be converted to type R. std::function
Use the template transformation constructor to receive the wrapped function object, in particular, the closure type can be implicitly converted to std::function
.
The simplest way to understand this is:
std::function
A new callable object is formed by encapsulation of various callable entities in C + +, such as normal functions, lambda expressions, function pointers, and other function objects std::function
; Let's not dwell on so many callable entities. Everything becomes simple and brutal.
How to use Std::function
std::function
the use of the feeling is "all the same," below the actual code examples to see how to use std::function
. Will be used is the kingly.
#include <functional>#include<iostream>using namespacestd;std::function<int(int) >functional;//Common FunctionsintTestFunc (inta) { returnA;}//lambda expressionAuto lambda = [] (inta)int{returnA;};//copy function (functor)classfunctor{ Public: int operator()(inta) {returnA; }};//1. Class member functions//2. Class static Functionsclasstestclass{ Public: intClassmember (intA) {returnA;} Static intStaticmember (intA) {returnA;}};intMain () {//Common FunctionsFunctional =TestFunc; intresult = Functional (Ten); cout<<"Common functions:"<< result <<Endl; //lambda expressionFunctional =Lambda; Result= Functional ( -); cout<<"lambda expression:"<< result <<Endl; //Imitation functionsFunctor Testfunctor; Functional=Testfunctor; Result= Functional ( -); cout<<"copy function:"<< result <<Endl; //class member functionsTestClass testobj; Functional= Std::bind (&Testclass::classmember, Testobj, std::p laceholders::_1); Result= Functional ( +); cout<<"class member functions:"<< result <<Endl; //class static FunctionsFunctional =Testclass::staticmember; Result= Functional ( -); cout<<"class static functions:"<< result <<Endl; return 0;}
For each callable entity to convert to a std::function
type of object, the code above has, run the code, read the above simple code. After summarizing the simple usage, take a look at some things to note:
- The following two principles apply to converting a callable entity to an
std::function
object:
- The
std::function
parameters of the converted object can be converted to the parameters of the callable entity;
- The return value of the callable entity can be converted to
std::function
the return value of the object.
std::function
The greatest use of an 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.
Why do you use Std::function
Useful and useful things will be added to the standard. We only use it in the project because it is useful and practical. std::function
implements a set of type elimination mechanisms that can handle different types of function objects uniformly. We used to do this with function pointers, and now we can use more secure std::function
to accomplish these tasks.
And why? I don't know, and why? And later found a better practical application example to come back and say why.
Summarize
C++11 's addition, seemingly let C + + changed a language, also has a lambda expression, a lot of things to learn, a lot of new language features. Oh, take it slow.
Std::function in "Turn" c++11