std::function

std::function 是C++中一种通用的多态函数对象封装器,可存储、拷贝和调用各种类型的函数、lambda表达式等。它满足CopyConstructible和Assignable要求。成员函数包括operator(),用于调用存储的可调用对象。当std::function没有存储目标时,调用会抛出异常。注意,当std::function的返回类型是引用,且由无状态lambda初始化时,返回的引用会绑定到临时对象,生命周期仅限于该次调用。
摘要由CSDN通过智能技术生成

std::function

定义于头文件

template<class>
class function; /*未定义*/

template < class R, class... Args>
class function<R(Args...)>;

类模板 std::function 是一种通用目的的多态函数封装器。std::function实例能够存储,拷贝,和调用任何CopyConstructible Callable 的对象–函数,lambda 表达式,bind 表达式,或者其他函数对象,和成员函数指针以及数据成员指针。

存储的可调用对象被称为std::functiontarget。如果一个 std::function 不包含目标,它被称为empty。调用一个 empty std::functiontarget 会导致std::bad_function_call异常抛出。std::function 满足CopyConstructibleCopyAssignable 的要求。

Member function

operator()
R operator()(Args... args) const;

使用参数args调用存储的可调用函数目标。
执行INVOKE<R>(f, std::forward<Args>(args)...), f 是 *this 的目标对象,INVOKE 是在 Callable 中描述的操作。

Parameter

args - 存储的可调用函数对象的传参

Return value

R 为 void 是无任何返回值,否则是存储的可调用对象的调用返回值。

Exceptions
  • std:;bad_function_call 如果 *this 没有存储任何可调用函数 target,i.e. !*this == true
Example
#include <iostream>
#include <functional>
 
void call(std::function<int()> f)  // can be passed by value
{ 
    std::cout << f() << '\n';
}
 
int normal_function() 
{
    return 42;
}
 
int main()
{
    int n = 1;
    std::function<int()> f = [&n](){ return n; };
    call(f);
 
    n = 2;
    call(f); 
 
    f = normal_function; 
    call(f);
}

Notes

当一个std::function的返回结果类型是引用类型,且其从一个没有 trailing-return-type 的 lambda表达式进行初始化。由于自动扣除的方式起了作用,这样的 lambda 表达式将总是返回一个纯右值。因此,结果引用将通常绑定到一个临时量中,其生命周期在 std::function::operator()返回时结束。

std::function<const int&()> F([]{ return 42; });
int x = F(); // Undefined behavior: the result of F() is a dangling reference

Examples

#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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值