function template
<functional>

std::relational operators (function)

(1)
template <class Ret, class... Args>  bool operator== (const function<Ret(Args...)>& lhs, nullptr_t rhs) noexcept;template <class Ret, class... Args>  bool operator== (nullptr_t lhs, const function<Ret(Args...)>& rhs) noexcept;
(2)
template <class Ret, class... Args>  bool operator!= (const function<Ret(Args...)>& lhs, nullptr_t rhs) noexcept;template <class Ret, class... Args>  bool operator!= (nullptr_t lhs, const function<Ret(Args...)>& rhs) noexcept;
Relational operators for function vs null pointer
Performs the appropriate comparison against null pointers:

equality comparison (1)
Returns true if the function object is an empty function (i.e., it has no target).
inequality comparison (2)
Returns true if the function object is not an empty function (i.e., it has a target).

Note that two function objects cannot be compared directly using these operators.

Parameters

lhs, rhs
A function object and a null pointer, in either order.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
// function comparisons vs nullptr
#include <iostream>     // std::cout
#include <functional>   // std::function, std::plus

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

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

  return 0;
}

Output:
foo is callable
bar is not callable


Return Value

true if the condition holds, and false otherwise.

Data races

The function object involved is accessed.

Exception safety

No-throw guarantee: never throws exceptions.

See also