The C Standard Library
The C++ Standard Library
The C++ STL Library
C++ Programming Resources
Selected Reading
The C++ Standard Library
- C++ Library - <valarray>
- C++ Library - <utility>
- C++ Library - <typeinfo>
- C++ Library - <tuple>
- C++ Library - <thread>
- C++ Library - <string>
- C++ Library - <stdexcept>
- C++ Library - <regex>
- C++ Library - <numeric>
- C++ Library - <new>
- C++ Library - <memory>
- C++ Library - <locale>
- C++ Library - <limits>
- C++ Library - <functional>
- C++ Library - <exception>
- C++ Library - <complex>
- C++ Library - <atomic>
- C++ Library - <streambuf>
- C++ Library - <sstream>
- C++ Library - <ostream>
- C++ Library - <istream>
- C++ Library - <iostream>
- C++ Library - <iosfwd>
- C++ Library - <ios>
- C++ Library - <iomanip>
- C++ Library - <fstream>
- C++ Library - Home
The C++ STL Library
- C++ Library - <iterator>
- C++ Library - <algorithm>
- C++ Library - <vector>
- C++ Library - <unordered_set>
- C++ Library - <unordered_map>
- C++ Library - <stack>
- C++ Library - <set>
- C++ Library - <queue>
- C++ Library - <map>
- C++ Library - <list>
- C++ Library - <forward_list>
- C++ Library - <deque>
- C++ Library - <bitset>
- C++ Library - <array>
C++ Programming Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
C++ Library - <functional>
C++ Library - <functional>
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 << ; } int num_; }; void print_num(int i) { std::cout << i << ; } struct PrintNum { void operator()(int i) const { std::cout << i << ; } }; 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) << ; 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 pke 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 | 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 vapd target is contained | |
7 | It is used to invoke the target |
Non-member functions
Sr.No. | Non-member functions | Definition |
---|---|---|
1 | It speciapzes the std::swap algorithm | |
2 | 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 spanision function object class | |
4 | It is a function object class for equapty comparison | |
5 | It is a function object class for greater-than inequapty 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 inequapty 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 multippcation function object class | |
15 | It is a negative function object class | |
16 | It is a function object class for non-equapty comparison | |
17 | It is an addition function object class |