首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

operators (std::function)

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

(1)

(since C++11)

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

(2)

(since C++11)

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

(3)

(since C++11)

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

(4)

(since C++11)

比较std::function带空指针。空函数%28,即没有可调用目标的函数%29比较相等,非空函数比较非相等。

参数

f

-

std::function to compare

返回值

1-2%29!f

3-4%29(bool) f

例外

noexcept规格:

noexcept

二次

代码语言:javascript
复制
#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) { // specialized compare with 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);
}

二次

产出:

二次

代码语言:javascript
复制
7
8

二次

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com