命名空间
变体
操作

std::function

来自 cppreference.cn
< cpp‎ | utility‎ | functional
 
 
实用工具库
通用工具
函数对象
位操作 (C++20 起)
(C++11)
关系运算符 (已弃用于 C++20)
整数比较函数
(C++20 起)(C++20 起)(C++20 起)  
(C++20 起)(C++20 起)(C++20 起)
(C++20)
交换类型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
常用词汇类型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
函数对象
函数包装器
function
(C++11)
(C++11)
函数调用
(C++17 起)(C++23 起)
恒等函数对象
(C++20)
引用包装器
(C++11 起)(C++11 起)
(C++20 起)(C++20 起)
透明运算符包装器
(C++14)
(C++14)
(C++14)
(C++14 起)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

旧式绑定器和适配器
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
 
 
定义于头文件 <functional>
template< class >
class function; /* 未定义 */
(自 C++11 起)
template< class R, class... Args >
class function<R(Args...)>;
(自 C++11 起)

类模板 std::function 是通用的多态函数包装器。std::function 的实例能存储、复制和调用任何 CopyConstructible Callable target —— 函数(通过指向它的指针)、lambda 表达式bind 表达式,或其他函数对象,以及指向成员函数的指针和指向数据成员的指针。

存储的可调用对象被称为 std::function目标 (target)。若 std::function 不含目标,则它被称作 (empty)。调用 std::function目标将导致抛出 std::bad_function_call 异常。

std::function 满足 CopyConstructibleCopyAssignable 的要求。

目录

[编辑] 成员类型

类型 定义
result_type R
argument_type
(已弃用于 C++17)(移除于 C++20)
Tsizeof...(Args)==1TArgs... 中的首个也是唯一类型
first_argument_type
(已弃用于 C++17)(移除于 C++20)
T1sizeof...(Args)==2T1Args... 中两个类型中的首个
second_argument_type
(已弃用于 C++17)(移除于 C++20)
T2sizeof...(Args)==2T2Args... 中两个类型中的第二个

[编辑] 成员函数

构造新的 std::function 实例
(公有成员函数) [编辑]
销毁 std::function 实例
(公有成员函数) [编辑]
赋值新目标
(公有成员函数) [编辑]
交换内容
(公有成员函数) [编辑]
(移除于 C++17)
赋值新目标
(公有成员函数) [编辑]
检查是否含有目标
(公有成员函数) [编辑]
调用目标
(公有成员函数) [编辑]
目标访问
获得存储目标的 typeid
(公有成员函数) [编辑]
获得指向存储目标的指针
(公有成员函数) [编辑]

[编辑] 非成员函数

特化 std::swap 算法
(函数模板) [编辑]
(移除于 C++20)
std::functionnullptr 比较
(函数模板) [编辑]

[编辑] 辅助类

(C++11 起) (直到 C++17)
特化 std::uses_allocator 类型特征
(类模板特化) [编辑]

[编辑] 推导指引(自 C++17 起)

[编辑] 注释

当从不带尾随返回类型 (trailing-return-type) 的 lambda 表达式初始化结果类型为引用的 std::function 时,应当谨慎。由于 auto 推导的工作方式,这种 lambda 表达式将始终返回 prvalue。因此,结果引用通常将绑定到临时对象,其生命周期在 std::function::operator() 返回时结束。

(直到 C++23)

若从返回 prvalue 的函数或函数对象(包括不带尾随返回类型 (trailing-return-type) 的 lambda 表达式)初始化返回引用的 std::function,则程序为非良构,因为禁止将返回的引用绑定到临时对象。

(自 C++23 起)
std::function<const int&()> F([] { return 42; }); // Error since C++23: can't bind
                                                  // the returned reference to a temporary
int x = F(); // Undefined behavior until C++23: the result of F() is a dangling reference
 
std::function<int&()> G([]() -> int& { static int i{0x2A}; return i; }); // OK
 
std::function<const int&()> H([i{052}] -> const int& { return i; }); // OK

[编辑] 示例

#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) << ";  ";
    std::cout << '\n';
}

可能输出

-9
42
31337
314160
314160
num_: 314159
314161
314162
18
5! = 120;  6! = 720;  7! = 5040;

[编辑] 参见

任何可调用对象 (callable object) 的仅移动包装器,它在给定的调用签名中支持限定符
(类模板) [编辑]
任何可复制构造可调用对象 (copy constructible callable object) 的可复制包装器,它在给定的调用签名中支持限定符
(类模板) [编辑]
任何可调用对象 (callable object) 的非占有包装器
(类模板) [编辑]
当调用空的 std::function 时抛出的异常
(类) [编辑]
(C++11)
从成员指针创建函数对象
(函数模板) [编辑]
typeid 查询类型信息,返回表示该类型的 std::type_info 对象