C++ 参考手册
- C++11
 - C++14
 - C++17
 - C++20
 - C++ 编译器支持情况表
 - 独立与宿主实现
 - C++ 语言
 - C++ 关键词
 - 预处理器
 - C++ 标准库头文件
 - 具名要求
 - 功能特性测试 (C++20)
 - 工具库
 - 程序支持工具
 - std::initializer_list
 - 函数对象
 - std::less
 - std::mem_fn
 - std::bind
 - std::function
 - std::not_fn
 - std::bind_front
 - std::function 的推导指引
 - std::swap(std::function)
 - std::uses_allocator<std::function>
 - std::function<R(Args...)>::assign
 - std::function<R(Args...)>::operator bool
 - std::function<R(Args...)>::operator()
 - std::function<R(Args...)>::target_type
 - std::function<R(Args...)>::target
 - operator==,!=(std::function)
 - std::function<R(Args...)>::operator=
 - std::function<R(Args...)>::swap
 - std::function<R(Args...)>::function
 - std::function<R(Args...)>::~function
 - std::bad_function_call
 - std::is_bind_expression
 - std::is_placeholder
 - std::placeholders::_1, std::placeholders::_2, ..., std::placeholders::_N
 - std::invoke
 - std::boyer_moore_searcher
 - std::default_searcher
 - std::identity
 - std::reference_wrapper
 - std::ref, std::cref
 - std::unwrap_reference, std::unwrap_ref_decay
 - std::plus
 - std::minus
 - std::negate
 - std::multiplies
 - std::divides
 - std::modulus
 - std::logical_and
 - std::logical_or
 - std::logical_not
 - std::equal_to
 - std::not_equal_to
 - std::greater
 - std::greater_equal
 - std::less_equal
 - std::bit_and
 - std::bit_or
 - std::bit_xor
 - std::bit_not
 - std::boyer_moore_horspool_searcher
 - std::unary_function
 - std::binary_function
 - std::ptr_fun
 - std::pointer_to_unary_function
 - std::pointer_to_binary_function
 - std::mem_fun_t, std::mem_fun1_t, std::const_mem_fun_t, std::const_mem_fun1_t
 - std::not1
 - std::not2
 - std::bind1st, std::bind2nd
 - std::mem_fun_ref_t, std::mem_fun1_ref_t, std::const_mem_fun_ref_t, std::const_mem_fun1_ref_t
 - std::unary_negate
 - std::binary_negate
 - std::less<void>
 - std::apply
 - 库特性测试宏 (C++20)
 - std::hash
 - std::pair
 - std::tuple
 - std::optional
 - std::any
 - std::variant
 - 格式化库 (C++20)
 - std::integer_sequence
 - std::exchange
 - std::make_from_tuple
 - std::launder
 - std::to_chars
 - std::from_chars
 - std::as_const
 - std::source_location
 - 变参数函数
 - std::bitset
 - std::cmp_equal, cmp_not_equal, cmp_less, cmp_greater, cmp_less_equal, cmp_greater_equal
 - std::in_range
 - std::declval
 - std::forward
 - std::move
 - std::move_if_noexcept
 - std::chars_format
 - std::piecewise_construct_t
 - std::piecewise_construct
 - std::in_place, std::in_place_type, std::in_place_index, std::in_place_t, std::in_place_type_t, std::in_place_index_t
 - 注释
 - 类型支持(基本类型、RTTI、类型特性)
 - 概念库 (C++20)
 - 错误处理
 - 动态内存管理
 - 日期和时间工具
 - 字符串库
 - 容器库
 - 迭代器库
 - 范围库 (C++20)
 - 算法库
 - 数值库
 - 输入/输出库
 - 文件系统库
 - 本地化库
 - 正则表达式库
 - 原子操作库
 - 线程支持库
 - 实验性 C++ 特性
 - 有用的资源
 - 索引
 - std 符号索引
 - 协程支持 (C++20)
 - C++ 关键词
 
位置:首页 > C++ 参考手册 >工具库 >函数对象 >std::function > operator==,!=(std::function)
operator==,!=(std::function)
|   定义于头文件  <functional>
  | 
||
|   template< class R, class... ArgTypes > bool operator==( const std::function<R(ArgTypes...)>& f,  | 
(1) | (C++11 起) | 
|   template< class R, class... ArgTypes > bool operator==( std::nullptr_t,  | 
(2) |  (C++11 起)  (C++20 前)  | 
|   template< class R, class... ArgTypes > bool operator!=( const std::function<R(ArgTypes...)>& f,  | 
(3) |  (C++11 起)  (C++20 前)  | 
|   template< class R, class... ArgTypes > bool operator!=( std::nullptr_t,  | 
(4) |  (C++11 起)  (C++20 前)  | 
与空指针比较 std::function 。空 function (即无可调用目标的 function )比较相等,非空 function 比较不相等。
参数
| f | - |  要比较的 std::function
 | 
返回值
1-2) !f
3-4) (bool) f
示例
运行此代码
#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) { // 与 nullptr 的特化比较 void_func_ = std::bind(&C::default_func, this, std::placeholders::_1); } void_func_(7); } 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); }
输出:
7 8