[modern c++] std::function的使用

本文详细介绍了C++11中的std::function和std::bind,它们用于创建和调整可调用对象。std::function可以封装函数、成员函数、lambda表达式,而std::bind则用于改变已有可调用对象的参数。通过示例展示了如何使用它们存储和调用不同类型的可调用对象,包括free function、lambda、成员函数和数据成员访问等。
摘要由CSDN通过智能技术生成

引言

c++ 11开始引入了对个可调用对象,比如 lambda,std::function,std::bind ,这些手段的目的只有一个,就是用来创建可调用对象,区别在于各自的适用场景不同,lambda用来创建一个临时可掉用对象,std::bind是一个函数,用来调整已有可调用对象的传参方式,并返回一个可调用对象。

std::function 是一个模板函数,可以用来包裹函数、成员函数、lambda或者可调用对象。

可以把std::function类比为一个函数指针,只不过这是一个可以实例化的指针。

std::bind和std::function最初都是boost库里的,后来才纳入到c++ 11中。

std::bind和std::function

bind 和 function 都可以用来创建可调用对象,bind主要用途是基于现有函数、可调用对象、lambda来创建,function更多情况下是用来表示一个可调用对象长什么样子。

一个典型的用法是:使用vector和function定义一个可调用对象数组,然后使用bind将各种不同入参和返回值组合的函数、lambda、可调用对象封装成统一的function实例,最后纳入vector。


C++11: std::function and std::bind | De C++ et alias OOPscenitatesstd::function and std::bind were born inside the Boost C++ Library, but they were incorporated into the new C++11 standard. std::function is a STL template class that provides a very convenient wrapper to a simple function, to a functor or to a lambda expression. For example, if you want to store several functions, functors or lambda…https://oopscenities.net/2012/02/24/c11-stdfunction-and-stdbind/

Demo:

#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()
{
    // store a free function
    std::function<void(int)> f_display = print_num;
    f_display(-9);
 
    // store a 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();
 
    // 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);
 
    // 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);
 
    // 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);
 
    auto factorial = [](int n) {
        // store a lambda object to emulate "recursive lambda"; aware of extra overhead
        std::function<int(int)> fac = [&](int n){ return (n < 2) ? 1 : n*fac(n-1); };
        // note that "auto fac = [&](int n){...};" does not work in recursive calls
        return fac(n);
    };
    for (int i{5}; i != 8; ++i) { std::cout << i << "! = " << factorial(i) << ";  "; }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值