operator== operator!=
描述 (Description)
它将std :: function与空指针进行比较。 空函数(即没有可调用目标的函数)比较相等,非空函数比较不相等。
声明 (Declaration)
以下是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 )
参数 (Parameters)
f - 用于比较函数。
返回值 (Return Value)
没有
异常 (Exceptions)
noexcep - 它不会抛出任何异常。
例子 (Example)
在下面的例子中解释了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