Namespaces
Variants

operator==,!= (std::function)

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
定义于头文件 <functional>
template < class R, class ... ArgTypes >

bool operator == ( const std:: function < R ( ArgTypes... ) > & f,

std:: nullptr_t ) noexcept ;
(1) (C++11 起)
template < class R, class ... ArgTypes >

bool operator == ( std:: nullptr_t ,

const std:: function < R ( ArgTypes... ) > & f ) noexcept ;
(2) (C++11 起)
(C++20 前)
template < class R, class ... ArgTypes >

bool operator ! = ( const std:: function < R ( ArgTypes... ) > & f,

std:: nullptr_t ) noexcept ;
(3) (C++11 起)
(C++20 前)
template < class R, class ... ArgTypes >

bool operator ! = ( std:: nullptr_t ,

const std:: function < R ( ArgTypes... ) > & f ) noexcept ;
(4) (C++11 起)
(C++20 前)

std::function 与空指针进行比较。空函数(即无可调用目标的函数)比较结果为相等,非空函数比较结果为不相等。

!= 运算符由 operator== 合成生成

(C++20 起)

目录

参数

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

参见

(C++23)
比较 std::move_only_function nullptr
(函数)