Std::function and Std::bind detailed

Source: Internet
Author: User
std::function

Std::function included in the header file #include <functional>, the various callable entities can be encapsulated and unified, including

Common function lambda expression function pointer imitation function (functor overloaded bracket operator implementation) class member function static member function

The following example implements a simple comparison of two-number functions in several ways:

#include <iostream>
#include <functional>

using namespace std;

Std::function<bool (int, int) > fun;

1. Normal function

normal function
bool compare_com (int a, int b)
{return
    a > b;
}

2.lambda expression

Lambda expression
Auto Compare_lambda = [] (int a, int b) {return a > b;};

3. Imitation function

Imitation function
class Compare_class
{public
:
    bool Operator () (int a, int b)
    {return
        a > b;
    }   
};

4. class member function (dynamic, static)

Class member function
class compare
{public
:
    bool Compare_member (int a, int b)
    {return
        a > b;
    }
    static bool Compare_static_member (int a, int b)
    {return
        a > b;
    }}
;

The corresponding main function is as follows:

int main ()
{
    bool result;
    fun = compare_com;
    result = Fun (1);
    cout << "normal function output, result are" << result << Endl;

    fun = Compare_lambda;
    result = Fun (1);
    cout << "lambda expression output, result was" << result << Endl;

    Fun = Compare_class ();
    result = Fun (1);
    cout << "Imitation function output, result is" << result << Endl;

    fun = Compare::compare_static_member;
    result = Fun (1);
    cout << "Class static member function output, result was" << result << Endl;

    Class ordinary member functions are special, need to use the BIND function, and need to instantiate the object, the member function to add the address character
    compare temp;
    Fun = Std::bind (&compare::compare_member, TEMP, std::p laceholders::_1, std::p laceholders::_2);
    result = Fun (1);
    cout << "Class ordinary member function output, result are" << result << Endl;
Here's an introduction to the Std::bind function

The Std::bind function binds the callable object (the first 6 classes) and the parameters of the callable object, returns the new callable object (the Std::function type, the argument list may change), and returns the new std:: The parameter list of a function callable object is determined according to the parameters of the std::p laceholders::_x from small to large in the BIND function argument. The following is an example of a functor binding, which enables you to compare the number of inputs to less than 3:

Bind function
    std::function<bool (int) > fun2;
    Returns a new Callable object argument list with only one int,std::p laceholders::_1 represents Compare_class () the first parameter
    fun2 = Std::bind (Compare_class (), 3, std:: placeholders::_1);
    result = Fun2 (3);
    cout << "bind function test, result was" << result << Endl;
return 0;
}

Here, re-write a detailed description of the new std::function that returns the parameter list of the callable object how to determine:

#include <functional>
#include <iostream>

using namespace std;
struct int 
{
    int A;
};

BOOL Compare_com (struct Int A, float b)
{return
    a.a > b;
}

int main ()
{
    int a = {1};
    Placeholders::_1 corresponds to float, placeholders::_2 corresponds to struct Int so the type of return value fun is Function<bool (float, Int) >
    std:: Function<bool (float, struct Int) > fun = Bind (compare_com, placeholders::_2, placeholders::_1);
    BOOL result = Fun (2.0, a);
    cout << "result are" << result << Endl;
    return 0;
}


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.