std::function
來自 cppreference.com
定義於標頭檔案 <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)==1 且 T 是 Args... 中的第一個也是唯一型別,則為 T |
first_argument_type (在 C++17 中已棄用)(在 C++20 中已移除) |
如果 sizeof...(Args)==2 且 T1 是 Args... 中兩個型別中的第一個,則為 T1 |
second_argument_type (在 C++17 中已棄用)(在 C++20 中已移除) |
如果 sizeof...(Args)==2 且 T2 是 Args... 中兩個型別中的第二個,則為 T2 |
[編輯] 成員函式
構造一個新的 std::function 例項(公共成員函式) | |
銷燬一個 std::function 例項(公共成員函式) | |
賦值一個新的目標 (公共成員函式) | |
交換內容 (公共成員函式) | |
(在 C++17 中已移除) |
賦值一個新的目標 (公共成員函式) |
檢查是否包含目標 (公共成員函式) | |
呼叫目標 (公共成員函式) | |
目標訪問 | |
獲取儲存目標的 typeid (公共成員函式) | |
獲取指向儲存目標的指標 (公共成員函式) |
[編輯] 非成員函式
(C++11) |
特化 std::swap 演算法 (函式模板) |
(在 C++20 中移除) |
比較 std::function 與 nullptr (函式模板) |
[編輯] 輔助類
(C++11) (直到 C++17) |
特化 std::uses_allocator 型別特性 (類模板特化) |
[編輯] 推導指南(從 C++17 開始)
[編輯] 注意
當 |
(直至 C++23) |
如果返回引用的 |
(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;
[編輯] 參閱
(C++23) |
任何支援給定呼叫簽名中限定符的可呼叫物件的僅移動包裝器 (類模板) |
(C++26) |
任何可複製構造的可呼叫物件的包裝器,支援給定呼叫簽名中的限定符 (類模板) |
(C++26) |
任何可呼叫物件的非擁有包裝器 (類模板) |
(C++11) |
呼叫空的 std::function 時丟擲的異常 (類) |
(C++11) |
從指向成員的指標建立函式物件 (函式模板) |
typeid | 查詢型別資訊,返回表示該型別的 std::type_info 物件 |