public member function
<functional>

std::function::operator bool

explicit operator bool() const noexcept;
Check if callable
Returns whether the object is callable.

A function object is callable if it is not an empty function (i.e., if it has a callable object as target).

Parameters

none

Return value

true if the object is callable.
false otherwise (the object is an empty function).

Example

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

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

  foo.swap(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

The object is accessed.

Exception safety

Strong guarantee: if an exception is thrown, there are no changes in the object.

See also