std::function

来自cppreference.com

 
 
实用工具库
类型的支持 (basic types, RTTI, type traits)
动态内存管理
错误处理
程序实用工具
可变参数函数
日期和时间
函数对象
initializer_list(C++11)
bitset
hash(C++11)
关系运算符
Original:
Relational operators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rel_ops::operator!=
rel_ops::operator>
rel_ops::operator<=
rel_ops::operator>=
双和元组
Original:
Pairs and tuples
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pair
tuple(C++11)
piecewise_construct_t(C++11)
piecewise_construct(C++11)
掉期,远期和移动
Original:
Swap, forward and move
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
swap
forward(C++11)
move(C++11)
move_if_noexcept(C++11)
declval(C++11)
 
函数对象
功能包装
Original:
Function wrappers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
function(C++11)
mem_fn(C++11)
bad_function_call(C++11)
绑定
Original:
Bind
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
bind(C++11)
is_bind_expression(C++11)
is_placeholder(C++11)
_1, _2, _3, ...(C++11)
参考包装
Original:
Reference wrappers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
reference_wrapper(C++11)
ref
cref
(C++11)
(C++11)
操作包装
Original:
Operator wrappers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
否定词
Original:
Negators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
已过时粘合剂和适配器
Original:
Deprecated binders and adaptors
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unary_function(过时了)
binary_function(过时了)
ptr_fun(过时了)
pointer_to_unary_function(过时了)
pointer_to_binary_function(过时了)
mem_fun(过时了)
mem_fun_t
mem_fun1_t
const_mem_fun_t
const_mem_fun1_t
(过时了)
(过时了)
(过时了)
(过时了)
mem_fun_ref(过时了)
mem_fun_ref_t
mem_fun1_ref_t
const_mem_fun_ref_t
const_mem_fun1_ref_t
(过时了)
(过时了)
(过时了)
(过时了)
binder1st
binder2nd
(过时了)
(过时了)
bind1st
bind2nd
(过时了)
(过时了)
 
std::function
成员函数
Original:
Member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
function::function
function::~function
function::operator=
function::swap
function::assign
function::operator bool
function::operator()
function::target_type
function::target
非成员函数
Original:
Non-member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
operator==
operator!=
std::swap(std::function)
Helper类
Original:
Helper classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
std::uses_allocator<std::function>
 
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.

目录

[编辑] 会员类型

类型
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)==1TArgs...是第一个也是唯一的类型
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)==2T1Args...中的两种类型是第一个
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)==2T2Args...中的两种类型的第二个
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.

(公共成员函数) [edit]
销毁一个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.

(公共成员函数) [edit]
分配的内容
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.

(公共成员函数) [edit]
交换的内容
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.

(公共成员函数) [edit]
分配一个新的目标
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.

(公共成员函数) [edit]
检查,如果包含一个有效的目标
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.

(公共成员函数) [edit]
调用的目标
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.

(公共成员函数) [edit]
目标访问
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.

(公共成员函数) [edit]
获得一个指针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.

(公共成员函数) [edit]

[编辑] 非成员函数

专业的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.

(函数模板) [edit]
比较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.

(类模板专业化) [edit]

[编辑] 为例

#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

[编辑] 另请参阅

调用一个空的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.

(类) [edit]
(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.

(函数模板) [edit]