std::function 和 std::bind

Callables

  1. It is a function pointer
  2. Having an operator () member function of the class object (functor)
  3. Is a class object may be mounted to change the function pointer
  4. He is a member of a class (function) pointers
void func()
{

}

struct Foo
{
    void operator()(void)
    {
        
    }
};

struct Bar
{
    using fr_t = void(*)(void);
    static void func(void)
    {
    }
    
    operator fr_t(void)
    {
        return func;
    }
};

struct A
{
    int a_;
    void mem_func(void)
    {
    }
};

int main()
{
    void(*func_ptr)(void) = &func;            //函数指针
    func_ptr();

    Foo foo;                                                        //仿函数
    foo();    
    
    Bar bar;
    bar();                                                             //可被转换为函数指针的类的对象

    void(A::*mem_func_ptr)(void) = &A::mem_func;        //类成员函数指针
    int A::*mem_obj_ptr = &A::a_;                                            //类成员指针

    A aa;
    (aa.*mem_func_ptr)();
    aa.*mem_obj_ptr = 123;

    return 0;
}

std::function

  std :: function call object is a wrapper that can accommodate all calls except class member objects (function) of the pointer. Template by specifying its parameters, it can handle functions, function objects, function pointer in a uniform manner, and allows saving and delay execution thereof.

#include <iostream>
#include <functional>

using namespace std;

void func(void)
{
    std::cout << __FUNCTION__ << std::endl;
}

class Foo
{
public:
    static int foo_func(int a)
    {
        std::cout << __FUNCTION__ << std::endl;
        return a;
    }
};

class Bar
{
public:
    void operator()(void)
    {
        std::cout << __FUNCTION__ << std::endl;
    }
};

class A
{
    std::function<void()> callback_;
public:
    A(const std::function<void()>& f):callback_(f)
    {
        
    }

    void notify(void)
    {
        callback_();
    }
};

void call_when_even(int x, const std::function<void(int)>& f)
{
    if(!(x & 1))
    {
        f(x);
    }
}

void output(int x)
{
    std::cout << x << " ";
}

int main()
{
    std::function<void(void)> fr1 = func;           //普通函数
    fr1();

    std::function<int(int)> fr2 = Foo::foo_func;    //类的静态成员函数
    std::cout << fr2(123) << std::endl;

    Bar bar;                                        //仿函数
    fr1 = bar;
    fr1();

    A aa(bar);
    aa.notify();

    for (size_t i = 0; i < 10; i++)
    {
        call_when_even(i, output);
    }
    std::cout << std::endl;
    
    return 0;
}

std::bind

  std :: bind will be used to call the object and its parameters to bind together. Results of the binding can be saved using std :: function, and delay any call to our time of need

  1. The parameter object and its callable bound together into a functor
  2. The polyol (number of parameters n, n> 1) can be transferred into a call to an object or element (n-1) callable element, i.e. bound only some parameters
class B
{
public:
    std::string name_;
    void output(int x, int y)
    {
        std::cout << x << " " << y << std::endl;
    }
};

int main()
{
    B b;
    std::function<void(int,int)> fr = std::bind(&B::output, &b, std::placeholders::_1, std::placeholders::_2);
    fr(1,2);

    std::function<std::string&(void)> fr_s = std::bind(&B::name_, &b);
    fr_s() = "hello";

    std::cout << b.name_ << std::endl;
    
    return 0;
}

Content from: C ++ 11-depth application code optimization and application-level project

Guess you like

Origin www.cnblogs.com/s3320/p/12120489.html