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 the 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, as the temperature is known new.
Std::function Introduction
class template std::function is a generic, polymorphic function encapsulation. An instance of Std::function can store, copy, and invoke operations on any target entity that can be invoked, including ordinary functions, lambda expressions, function pointers, and other function objects. The Std::function object is a type-safe package for existing callable entities in C + + (we know that callable entities such as function pointers are of type unsafe).
Usually Std::function is a function object class that wraps any other function object, the wrapped function object has a type of T1, ..., TN's n parameters, and returns a value that can be converted to R type. Std::function uses template transformation constructors to receive wrapped function objects; In particular, the closure type can be implicitly converted to std::function.
The simplest understanding is:
A new callable Std::function object is formed by std::function the encapsulation of various callable entities (ordinary functions, lambda expressions, function pointers, and other function objects) in C + +; Let's not tangle with so many callable entities. Everything becomes simple and rough.
Things that are useful and useful can only be added to the standard. Because it is easy to use, practical, we are using it in the project. Std::function implements a set of type elimination mechanisms that can unify different types of function objects. We used to do this with function pointers, and now we can use a more secure std::function to do these tasks.
Example
#include <functional> #include <iostream> struct foo {foo (int num): num_ (num) {} void Print_add (int i
const {std::cout << num_+i << ' \ n ';}
int num_;
};
void Print_num (int i) {std::cout << i << ' \ n ';}
struct Printnum {void operator () (int i) const {std::cout << i << ' \ n ';
}
};
int main () {//Save free function std::function<void (int) > F_display = print_num;
F_display (-9);
Save lambda Expression std::function<void () > f_display_42 = [] () {print_num (42);};
F_display_42 ();
Save Std::bind Results std::function<void () > f_display_31337 = Std::bind (Print_num, 31337);
f_display_31337 ();
Save member functions std::function<void (const foo&, int) > F_add_display = &foo::p rint_add;
Foo foo (314159);
F_add_display (foo, 1);
Save member functions and objects using std::p laceholders::_1;
std::function<void (int) > F_add_display2= std::bind (&foo::p rint_add, Foo, _1);
F_add_display2 (2); // Save member functions and object pointers std::function<void (int) > f_add_display3= std::bind (&foo::p rint_add, &foo, _1);
F_add_display3 (3);
Save function Object Std::function<void (int) > f_display_obj = Printnum ();
F_display_obj (18);
}
Output:
-9
31337
314160
314161
314162
18