std::function
来自cppreference.com
< cpp | utility | functional
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <functional>
|
||
| template< class > class function; /* undefined */ |
(C++11 起) | |
| template< class R, class... Args > class function<R(Args...)> |
(C++11 起) | |
std::function类模板是一个通用的多态函数的包装。 std::function的实例存储,复制,和调用任何可调用的“目标” - 的功能,lambda表达式,绑定表达式,或其他的函数对象. Original:
Class template
std::function is a general-purpose polymorphic function wrapper. Instances of std::function can store, copy, and invoke any callable target -- functions, lambda表达式, 绑定表达式, or other function objects. The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 会员类型
| 类型
Original: Type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
Definition |
result_type
|
R
|
argument_type
|
T如果sizeof...(Args)==1T在Args...是第一个也是唯一的类型 Original: T if sizeof...(Args)==1 and T is the first and only type in Args... The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
first_argument_type
|
T1如果sizeof...(Args)==2和T1Args...中的两种类型是第一个 Original: T1 if sizeof...(Args)==2 and T1 is the first of the two types in Args... The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
second_argument_type
|
T2如果sizeof...(Args)==2和T2Args...中的两种类型的第二个 Original: T2 if sizeof...(Args)==2 and T2 is the second of the two types in Args... The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[编辑] 成员函数
| std::function构造一个新的实例 Original: constructs a new std::function instance The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 销毁一个std::function实例 Original: destroys a std::function instance The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 分配的内容 Original: assigns the contents The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 交换的内容 Original: swaps the contents The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 分配一个新的目标 Original: assigns a new target The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 检查,如果包含一个有效的目标 Original: checks if a valid target is contained The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 调用的目标 Original: invokes the target The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
Original: Target access The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
| 获得所存储的目标的typeidstd::function Original: obtains the typeid of the stored target of a std::function The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 获得一个指针std::function存储的目标的 Original: obtains a pointer to the stored target of a std::function The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
[编辑] 非成员函数
| (C++11) |
专业的std::swap算法 Original: specializes the std::swap algorithm The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) |
| 比较std::functionstd::nullptr Original: compares an std::function with std::nullptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |
[编辑] Helper类
| 专业的std::uses_allocator型特征 Original: specializes the std::uses_allocator type trait The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (类模板专业化) | |
[编辑] 为例
#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'; } 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; Foo foo(314159); f_add_display(foo, 1); }
Output:
-9 42 31337 314160
[编辑] 另请参阅
| (C++11) |
调用一个空的std::function时抛出的异常 Original: the exception thrown when invoking an empty std::function The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (类) |
| (C++11) |
创建一个函数对象的指针的成员 Original: creates a function object out of a pointer to a member The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) |