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.

参数

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

示例

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;
}

输出
foo is callable
bar is not callable


返回值

如果条件成立,则为 true;否则为 false

数据竞争

The function object involved is accessed.

异常安全

无异常保证: 绝不抛出异常。

另见