Std::function in "Turn" c++11

Source: Internet
Author: User

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::functionWhat 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::functioninstance 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::functionUse 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::functionA 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::functionthe 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::functionThe 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::functionimplements 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

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.