std::function

std::function是一个通用多态函数包装器,该函数包装器模板能包装任何类型的可调用元素(callable element),例如普通函数,函数指针,成员函数(静态和非静态)和函数对象。

Class template std::function is a general-purpose polymorphic function wrapper. Instances of std::function can store, copy, and invoke any Callable target – functions, lambda expressions, bind expressions, or other function objects, as well as pointers to member functions and pointers to data members.

The stored callable object is called the target of std::function. If a std::function contains no target, it is called empty. Invoking the target of an empty std::function results in std::bad_function_call exception being thrown.

std::function satisfies the requirements of CopyConstructible and CopyAssignable.

代码示例(参考 https://en.cppreference.com/w/cpp/utility/functional/function):

#include <functional>
#include <iostream>

struct Foo {//类成员
    Foo(int num) : num_(num) {}
    void print_add(int i) const { std::cout << num_+i << '\n'; }
    void print_add2(int i,int j) const { std::cout << num_+i+j << '\n'; }
    static void print_static(int i)  { std::cout << i << '\n'; }
    int num_;
};

struct PrintNum {//函数对象(仿函数)
    void operator()(int i) const
    {
        std::cout << i << '\n';
    }
};
void print_num(int i) //普通非成员函数
{
    std::cout << i << '\n';
}
template <class T>
T add(T i, T j)
{
    return i + j;
}
int main()
{
    //存储add一个普通函数
    std::function<void(int)> f1 = print_num;
    f1(10);
    //存储包装一个模板函数
    auto f = add<int>;//std::function<int(int, int)> f
    cout << f(1, 2) << endl;

    //存储一个lambda表达式
    std::function<void()> f_display_42 = []() { print_num(42); };
    f_display_42();

    // store the result of a call to std::bind
    std::function<void()> f_display_31337 = std::bind(print_num, 31337);
    f_display_31337();
    //存储包装一个类静态成员函数
    std::function<void(int)> f2 = &Foo::print_static; //std::function<void(int)> f2 = &Foo::print_static<int>;
    f2(10);

    // store a call to a member function 成员函数
    std::function<void(const Foo&, int)> f_add_display = &Foo::print_add;
    const Foo foo(314159);
    f_add_display(foo, 1);
    f_add_display(314159, 1);//add explicit err

    // store a call to a data member accessor 成员变量
    std::function<int(Foo const&)> f_num = &Foo::num_;
    std::cout << "num_: " << f_num(foo) << '\n';

    // store a call to a member function and object
    using std::placeholders::_1; //参数绑定
    std::function<void(int)> f_add_display2 = std::bind( &Foo::print_add, foo, _1 );
    f_add_display2(2);
    //2 param
    const Foo foo1(0);
    function<void(int,int)> f3 = bind( &Foo::print_add2, foo1, placeholders::_1, placeholders::_2);
    f3(10,20);
    // store a call to a member function and object ptr
    std::function<void(int)> f_add_display3 = std::bind( &Foo::print_add, &foo, _1 );
    f_add_display3(3);

    // store a call to a function object  函数对象
    std::function<void(int)> f_display_obj = PrintNum();
    f_display_obj(18);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值