在线文档教程
C++
应用 | Utilities

std::function::operator bool

STD::功能::操作员bool

explicit operator bool() const;(since C++11)

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

参数

%280%29

返回值

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

例外

noexcept规格:

noexcept

二次

#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 }

二次

产出:

二次

f1: Function is empty. Nothing to do. f2: Function is not empty! Calling function. This is the sample function!

二次

© cppreference.com

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

http://en.cpPreference.com/w/cpp/实用程序/FunctionalFunctioner/Operator[医]布尔