Cpp Standard Library 简明教程
C++ Library - <functional>
Introduction
函数对象是指专门用于与函数类似的语法一起使用的对象。std::function 的实例可以存储、复制和调用任何可调用的目标——函数、lambda 表达式、bind 表达式或其他函数对象以及成员函数指针和数据成员指针。
Function objects are objects specifically designed to be used with a syntax similar to that of functions. Instances of std::function can store, copy, and invoke any Callable target — functions, lambda expressions, bind expressions, or other function objects, as well as pointers to member functions and pointers to data members.
Declaration
以下是 std::function 的声明。
Following is the declaration for std::function.
template<class >
class function;
Parameters
-
R − result_type.
-
argument_type − T if sizeof…(Args)==1 and T is the first and only type in Args.
Example
以下是 std::function 的示例。
In below example for std::function.
#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() {
std::function<void(int)> f_display = print_num;
f_display(-9);
std::function<void()> f_display_42 = []() { print_num(42); };
f_display_42();
std::function<void()> f_display_31337 = std::bind(print_num, 31337);
f_display_31337();
std::function<void(const Foo&, int)> f_add_display = &Foo::print_add;
const Foo foo(314159);
f_add_display(foo, 1);
std::function<int(Foo const&)> f_num = &Foo::num_;
std::cout << "num_: " << f_num(foo) << '\n';
using std::placeholders::_1;
std::function<void(int)> f_add_display2= std::bind( &Foo::print_add, foo, _1 );
f_add_display2(2);
std::function<void(int)> f_add_display3= std::bind( &Foo::print_add, &foo, _1 );
f_add_display3(3);
std::function<void(int)> f_display_obj = PrintNum();
f_display_obj(18);
}
示例输出如下所示 −
The sample output should be like this −
-9
42
31337
314160
num_: 314159
314161
314162
18
Member functions
Sr.No. |
Member functions |
Definition |
1 |
It is used to construct a new std::function instance |
|
2 |
It is used to destroy a std::function instance |
|
3 |
[role="bare"]../cpp_standard_library/cpp_functional_operator.html |
It is used to assign a new target |
4 |
It is used to swap the contents |
|
5 |
It is used to assign a new target |
|
6 |
It is used to check if a valid target is contained |
|
7 |
It is used to invoke the target |
Non-member functions
Sr.No. |
Non-member functions |
Definition |
1 |
It specializes the std::swap algorithm |
|
2 |
[role="bare"]../cpp_standard_library/cpp_functional_operator_non_member.html |
It compares an std::function with nullptr |
Operator classes
Sr.No. |
Operator classes |
Definition |
1 |
It is a bitwise AND function object class |
|
2 |
It is a bitwise OR function object class |
|
3 |
It is a bitwise XOR function object class |
|
3 |
It is a division function object class |
|
4 |
It is a function object class for equality comparison |
|
5 |
It is a function object class for greater-than inequality comparison |
|
6 |
It is a function object class for greater-than-or-equal-to comparison |
|
7 |
It is a function object class for less-than inequality comparison |
|
8 |
It is a function object class for less-than-or-equal-to comparison |
|
9 |
It is a logical AND function object class |
|
10 |
It is a logical NOT function object class |
|
11 |
It is a logical OR function object class |
|
12 |
It is a subtraction function object class |
|
13 |
It is a modulus function object class |
|
14 |
It is a multiplication function object class |
|
15 |
It is a negative function object class |
|
16 |
It is a function object class for non-equality comparison |
|
17 |
It is an addition function object class |