public member function
<functional>

std::function::operator()

Ret operator()(Args... args) const;
Call target
Calls the target callable object, forwarding args as arguments.

The effect depends on the type of the callable object targeted by the function object:

  • If the target is a function pointer or a function object, it is called forwarding the arguments to the call.
  • If the target is a pointer to a non-static member function, it is called using the first argument as the object on which the member is called (this may either be an object, a reference, or a pointer to it), and the remaining arguments are forwarded as arguments for the member function.
  • If it is a pointer to a non-static data member, it should be called with a single argument, and the function returns a reference to that member of its argument (the argument may either be an object, a reference, or a pointer to it).
  • If the object has no target (it is an empty function), it throws a bad_function_call exception.

Parameters

args...
Arguments for the call.
If the type of the callable object targeted by the function object is a member pointer, the first argument shall be an object for which that member is defined (or a reference, or a pointer to it).

Args... are the argument types specified in the signature used as class template parameter.

Return value

The result of the call as a value of type Ret.
If Ret is void, the function returns no value.

Ret is the return type specified in the signature used as class template parameter (aliased as member type function::result_type).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// function::operator() example
#include <iostream>     // std::cout
#include <functional>   // std::function, std::plus, std::minus, std::multiplies

int main () {
  // an array of functions:
  std::function<int(int,int)> fn[] = {
    std::plus<int>(),
    std::minus<int>(),
    std::multiplies<int>()
  };

  for (auto& x: fn) std::cout << x(10,5) << '\n';

  return 0;
}

Output:
15
5
50


Data races

Both the object and its function::target are accessed.

Exception safety

Provides the same level as a call to the target callable object.
If the function object is empty (i.e., it is not callable), a bad_function_call exception is thrown.

See also