C++ Library - <functional>


Advertisements

Introduction

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

Following is the declaration for std::function.

template<class >
class function; 

C++11

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

Parameters

  • R − result_type.

  • argument_type − T if sizeof...(Args)==1 and T is the first and only type in Args.

Example

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 operator= 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 operator== operator!= 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
Advertisements