名稱空間
變體
操作

std::function

來自 cppreference.com
 
 
 
函式物件
函式包裝器
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 物件