public member function
<functional>

std::swap (function)

template <class Ret, class... Args>  void swap (function<Ret(Args...)>& x, function<Ret(Args...)>& y);
Exchanges the targets
The target callable object held by x is exchanged with that of y.

It behaves as if x.swap(y) was called.

Parameters

x,y
function objects of the same type (with the same signature, as described by the template parameters Ret and Args...).

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// swapping functions
#include <iostream>     // std::cout
#include <functional>   // std::function, std::plus

int main () {
  std::function<int(int,int)> foo,bar;
  foo = std::plus<int>();

  swap(foo,bar);

  std::cout << "foo is " << (foo ? "callable" : "not callable") << ".\n";
  std::cout << "bar is " << (bar ? "callable" : "not callable") << ".\n";

  return 0;
}

Output:
foo is not callable.
bar is callable.


Data races

Both objects, x and y, are modified.

Exception safety

No-throw guarantee: never throws exceptions.

See also