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

std::function::operator bool

explicit operator bool() const;

(since C++11)

检查是否*this存储可调用的函数目标,即不为空。

参数

%280%29

返回值

true如果*this存储可调用函数目标,false否则。

例外

noexcept规格:

noexcept

二次

代码语言:javascript
复制
#include <functional>
#include <iostream>
 
void sampleFunction()
{
    std::cout << "This is the sample function!\n";
}
 
void checkFunc( std::function<void()> &func )
{
    // Use operator bool to determine if callable target is available.
    if( func )  
    {
        std::cout << "Function is not empty! Calling function.\n";
        func();
    }
    else
    {
        std::cout << "Function is empty. Nothing to do.\n";
    }
}
 
int main()
{
    std::function<void()> f1;
    std::function<void()> f2( sampleFunction );
 
    std::cout << "f1: ";
    checkFunc( f1 );
 
    std::cout << "f2: ";
    checkFunc( f2 );
}

二次

产出:

二次

代码语言:javascript
复制
f1: Function is empty. Nothing to do.
f2: Function is not empty! Calling function.
This is the sample function!

二次

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

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

扫码关注腾讯云开发者

领取腾讯云代金券