C++ Functional 库 - operator==,!=(std::function)
描述
它将 std::function 与空指针进行比较。 空函数(即没有可调用目标的函数)比较相等,非空函数比较不相等。
声明
以下是 std::function 的声明。
template< class R, class... ArgTypes > bool operator==( const std::function<R(ArgTypes...)>& f, std::nullptr_t )
C++11
template< class R, class... ArgTypes > bool operator==( const std::function<R(ArgTypes...)>& f, std::nullptr_t )
参数
f − 它用于在函数之间进行比较。
返回值
none
异常
noexcep − 它不会抛出任何异常。
示例
在下面的示例中解释了 std::function。
#include <functional>
#include <iostream>
using SomeVoidFunc = std::function<void(int)>;
class C {
public:
C(SomeVoidFunc void_func = nullptr) :
void_func_(void_func) {
if (void_func_ == nullptr) {
void_func_ = std::bind(&C::default_func, this, std::placeholders::_1);
}
void_func_(9);
}
void default_func(int i) { std::cout << i << '\n'; };
private:
SomeVoidFunc void_func_;
};
void user_func(int i) {
std::cout << (i + 1) << '\n';
}
int main() {
C c1;
C c2(user_func);
}
让我们编译并运行上面的程序,这将产生以下结果 −
9 10

