Namespaces
Variants

std::function<R(Args...)>:: function

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
function ( ) noexcept ;
(1) (自 C++11 起)
function ( std:: nullptr_t ) noexcept ;
(2) (自 C++11 起)
function ( const function & other ) ;
(3) (自 C++11 起)
(4)
function ( function && other ) ;
(自 C++11 起)
(直至 C++20)
function ( function && other ) noexcept ;
(自 C++20 起)
template < class F >
function ( F && f ) ;
(5) (自 C++11 起)
template < class Alloc >
function ( std:: allocator_arg_t , const Alloc & alloc ) noexcept ;
(6) (自 C++11 起)
(于 C++17 移除)
template < class Alloc >

function ( std:: allocator_arg_t , const Alloc & alloc,

std:: nullptr_t ) noexcept ;
(7) (自 C++11 起)
(于 C++17 移除)
template < class Alloc >

function ( std:: allocator_arg_t , const Alloc & alloc,

const function & other ) ;
(8) (自 C++11 起)
(于 C++17 移除)
template < class Alloc >

function ( std:: allocator_arg_t , const Alloc & alloc,

function && other ) ;
(9) (自 C++11 起)
(于 C++17 移除)
template < class F, class Alloc >
function ( std:: allocator_arg_t , const Alloc & alloc, F f ) ;
(10) (自 C++11 起)
(于 C++17 移除)

从多种来源构造一个 std::function

1,2) 创建一个 空的 std::function
3) other 目标 复制到 * this 的目标。
如果 other 为空, * this 在调用后也将为空。
4) other 的目标移动到 * this 的目标。
如果 other 为空, * this 在调用后也将为空。
other 在调用后立即处于有效但未指定的状态。
5) 使用 std:: forward < F > ( f ) 初始化目标。目标的类型为 std:: decay < F > :: type
f 是空函数指针、空成员指针,或某个 std::function 特化的空值,则在调用后 * this 将立即变为空值。
此重载仅在满足以下所有条件时参与重载决议:
(since C++23)
  • 类型为 std:: decay < F > :: type 的左值对于参数类型 Args... 和返回类型 R 是可调用的。

std:: is_copy_constructible_v < std:: decay_t < F >> std:: is_constructible_v < std:: decay_t < F > , F > false ,则程序非良构。

(since C++23)
如果 F 不满足 可复制构造 要求,则行为未定义。
6-10) (1-5) 相同,区别在于使用 alloc std::function 可能使用的任何内部数据结构分配内存。

当目标为函数指针或 std::reference_wrapper 时,保证进行小对象优化,即这些目标始终直接存储在 std::function 对象内部,不会发生动态内存分配。其他大型对象可能在动态分配的存储空间中构造,并通过指针由 std::function 对象进行访问。

目录

参数

other - 用于初始化 * this 的函数对象
f - 用于初始化 * this 的可调用对象
alloc - 用于内部内存分配的 Allocator
类型要求
-
Alloc 必须满足 Allocator 的要求。

异常

3,8,9) other 的目标为函数指针或 std::reference_wrapper 时不抛出异常,否则可能抛出 std::bad_alloc 或复制/移动存储的可调用对象时构造函数所抛出的任何异常。
4) other 的目标为函数指针或 std::reference_wrapper 则不抛出异常,否则可能抛出 std::bad_alloc 或复制/移动存储的可调用对象时构造函数所抛出的任何异常。
(C++20 前)
5,10) 如果 f 是函数指针或 std::reference_wrapper 则不抛出异常,否则可能抛出 std::bad_alloc 或存储的可调用对象拷贝构造函数所抛出的任何异常。

注释

std::function 的分配器支持曾存在规范不明确且实现不一致的问题。部分实现完全不提供重载版本 ( 6-10 ) ,部分实现虽提供重载但会忽略传入的分配器参数,还有部分实现在构造时使用传入的分配器但在 std::function 被重新赋值时不再使用。因此,C++17标准移除了分配器支持功能。

示例

#include <functional>
#include <iostream>
#include <utility>
void print_num(int i) { std::cout << "print_num(" << i << ")\n"; }
int main()
{
    std::function<void(int)> func1; // (1) 空构造函数
    try
    {
        func1(333 << 1);
    }
    catch (const std::bad_function_call& ex)
    {
        std::cout << "1) " << ex.what() << '\n';
    }
    std::function<void(int)> func2{nullptr}; // (2) 空构造函数
    try
    {
        func1(222 * 3);
    }
    catch (const std::bad_function_call& ex)
    {
        std::cout << "2) " << ex.what() << '\n';
    }
    func1 = print_num; // 使用赋值运算符初始化 func1
    std::function<void(int)> func3{func1}; // (3) 拷贝构造函数
    func3(33);
    std::function<void(int)> func4{std::move(func3)}; // (4) 移动构造函数,
                                                      // func3 处于未指定状态
    func4(44);
    std::function<void(int)> func5{print_num}; // (5) 函数指针构造函数
    func5(55);
    // (5) lambda 表达式构造函数
    std::function<void(int)> func6([](int i) { std::cout << "lambda(" << i << ")\n"; });
    func6(66);
}

可能的输出:

1) bad_function_call
2) bad_function_call
print_num(33)
print_num(44)
print_num(55)
lambda(66)

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用范围 发布时行为 正确行为
LWG 2132 C++11 重载版本 ( 5,10 ) 可能存在歧义 已添加约束
LWG 2774 C++11 ( 5,10 ) 执行了额外的移动操作 已消除

参见

构造新的 std::move_only_function 对象
( std::move_only_function 的公开成员函数)