目录

operator bool

描述 (Description)

它用于检查是否包含有效目标。

声明 (Declaration)

以下是std :: function :: function :: operator bool的声明。

explicit operator bool() const;

C++11

explicit operator bool() const;

参数 (Parameters)

没有

返回值 (Return Value)

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

异常 (Exceptions)

noexcept:它不会抛出任何异常。

例子 (Example)

在下面的示例中为std :: function :: operator bool。

#include <functional>
#include <iostream>
void sampleFunction() {
   std::cout << "This is the sample example of function!\n";
}
void checkFunc( std::function<void()> &func ) {
   if( func ) {
      std::cout << "Function is not empty! It is a calling function.\n";
      func();
   } else {
      std::cout << "Function is empty.\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.
f2: Function is not empty! It is a calling function.
This is the sample example of function!
↑回到顶部↑
WIKI教程 @2018