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;

C++11

template< class R, class... Args >
class function<R(Args...)>

Parameters

  1. R − result_type.

  2. 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

(constructor)

It is used to construct a new std::function instance

2

(destructor)

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

swap

It is used to swap the contents

5

assign

It is used to assign a new target

6

operator bool

It is used to check if a valid target is contained

7

operator()

It is used to invoke the target

Non-member functions

Sr.No.

Non-member functions

Definition

1

std::swap

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

bit_and

It is a bitwise AND function object class

2

bit_or

It is a bitwise OR function object class

3

bit_xor

It is a bitwise XOR function object class

3

divides

It is a division function object class

4

equal_to

It is a function object class for equality comparison

5

greater

It is a function object class for greater-than inequality comparison

6

greater_equal

It is a function object class for greater-than-or-equal-to comparison

7

less

It is a function object class for less-than inequality comparison

8

less_equal

It is a function object class for less-than-or-equal-to comparison

9

logical_and

It is a logical AND function object class

10

logical_not

It is a logical NOT function object class

11

logical_or

It is a logical OR function object class

12

minus

It is a subtraction function object class

13

modulus

It is a modulus function object class

14

multiplies

It is a multiplication function object class

15

negate

It is a negative function object class

16

not_equal_to

It is a function object class for non-equality comparison

17

plus

It is an addition function object class