public member function
<functional>

std::function::function

default/empty (1)
function() noexcept;
function (nullptr_t fn) noexcept;
initialization (2)
template <class Fn> function (Fn fn);
copy (3)
function (const function& x);
move (4)
function (function&& x);
with allocator (5)
template <class Alloc>
  function (allocator_arg_t aa, const Alloc& alloc) noexcept;
template <class Alloc>
  function (allocator_arg_t aa, const Alloc& alloc, nullptr_t fn) noexcept;
template <class Fn, class Alloc>
  function (allocator_arg_t aa, const Alloc& alloc, Fn fn);
template <class Alloc>
  function (allocator_arg_t aa, const Alloc& alloc, const function& x);
template <class Alloc>
  function (allocator_arg_t aa, const Alloc& alloc, function&& x);
Construct function wrapper
Constructs a function object:

(1) default / empty
Constructs an empty function object (with no target).
(2) initialization
The object stores a decayed copy of fn as its target.
fn shall be callable for the arguments and return type specified as template arguments for the class.
If fn is not callable for the arguments and return type specified as template arguments for the class, this constructor does not participate in overload resolution.
(3) copy constructor
The object stores a copy of x's target (x.target()).
(4) move constructor
The object acquires x's target.
x is left in an unspecified but valid state.
(5) versions with allocator
Same as the versions above, but the object stores alloc and uses it to allocate internal storage, if necessary.
Library implementations may optimize for small callable objects (such as when the target is a function pointer) and not use dynamically allocated memory for objects targeting these.

Parameters

fn
Either a function, a function pointer, a pointer to member, or any kind of copy-constructible function object (i.e., an object whose class defines operator(), including closures and other instantiations of function).
If fn is a null pointer, a null member pointer or an empty function object, the object is initialized as an empty function.
Otherwise, the object is initialized to target a decayed copy of fn (internally initialized with std::move(fn)).
x
A function object of the same type (with the same signature, as described by its template parameters) whose target is either copied or moved into *this.
If x is an empty function object, the object is initialized as an empty function.
aa
The std::allocator_arg value. This constant value is merely used to select the constructor forms with an allocator parameter.
alloc
Allocator object used to allocate internal memory, if necessary.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// function example
#include <iostream>     // std::cout
#include <functional>   // std::function, std::negate

// a function:
int half(int x) {return x/2;}

// a function object class:
struct third_t {
  int operator()(int x) {return x/3;}
};

// a class with data members:
struct MyValue {
  int value;
  int fifth() {return value/5;}
};

int main () {
  std::function<int(int)> fn1 = half;                    // function
  std::function<int(int)> fn2 = &half;                   // function pointer
  std::function<int(int)> fn3 = third_t();               // function object
  std::function<int(int)> fn4 = [](int x){return x/4;};  // lambda expression
  std::function<int(int)> fn5 = std::negate<int>();      // standard function object

  std::cout << "fn1(60): " << fn1(60) << '\n';
  std::cout << "fn2(60): " << fn2(60) << '\n';
  std::cout << "fn3(60): " << fn3(60) << '\n';
  std::cout << "fn4(60): " << fn4(60) << '\n';
  std::cout << "fn5(60): " << fn5(60) << '\n';

  // stuff with members:
  std::function<int(MyValue&)> value = &MyValue::value;  // pointer to data member
  std::function<int(MyValue&)> fifth = &MyValue::fifth;  // pointer to member function

  MyValue sixty {60};

  std::cout << "value(sixty): " << value(sixty) << '\n';
  std::cout << "fifth(sixty): " << fifth(sixty) << '\n';

  return 0;
}


Output:
fn1(60): 30
fn2(60): 30
fn3(60): 20
fn4(60): 15
fn5(60): -60
value(sixty): 60
fifth(sixty): 12

Exception safety

If the target is either a function pointer or a reference_wrapper to a callable object, it never throws exceptions (no-throw guarantee).
Otherwise, it can only throw if the copy- or move-construction of the target callable object throws, or if an exception (such as bad_alloc) is thrown allocating memory.

See also