命名空间
变体
操作

std::function

来自 cppreference.cn
 
 
 
函数对象
函数包装器
function
(C++11)
(C++11)
函数调用
(C++17)(C++23)
恒等函数对象
(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) 目标——函数(通过其指针)、lambda 表达式bind 表达式,或其他函数对象,以及指向成员函数的指针和指向数据成员的指针。

存储的可调用对象称为 std::function目标。如果 std::function 不包含目标,则称其为空的。调用空的 std::function目标会导致抛出 std::bad_function_call 异常。

std::function 满足可复制构造(CopyConstructible)可复制赋值(CopyAssignable)的要求。

目录

[编辑] 成员类型

类型 定义
result_type R
argument_type
(在 C++17 中已弃用)(在 C++20 中已移除)
如果 sizeof...(Args)==1TArgs... 中的第一个也是唯一类型,则为 T
first_argument_type
(在 C++17 中已弃用)(在 C++20 中已移除)
如果 sizeof...(Args)==2T1Args... 中两个类型中的第一个,则为 T1
second_argument_type
(在 C++17 中已弃用)(在 C++20 中已移除)
如果 sizeof...(Args)==2T2Args... 中两个类型中的第二个,则为 T2

[编辑] 成员函数

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

[编辑] 非成员函数

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

[编辑] 辅助类

特化 std::uses_allocator 类型特性
(类模板特化) [编辑]

[编辑] 推导指南(从 C++17 开始)

[编辑] 注意

std::function 的结果类型是引用,并且通过没有尾随返回类型的 lambda 表达式进行初始化时,需要注意。由于自动推导的工作方式,此类 lambda 表达式将始终返回一个纯右值。因此,生成的引用通常会绑定到一个临时对象,该临时对象的生命周期在 std::function::operator() 返回时结束。

(直至 C++23)

如果返回引用的 std::function 是通过返回纯右值的函数或函数对象(包括没有尾随返回类型的 lambda 表达式)初始化的,则程序是格式错误的,因为禁止将返回的引用绑定到临时对象。

(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;

[编辑] 参阅

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