名稱空間
變體
操作

std::function<R(Args...)>::operator bool

來自 cppreference.com
< cpp‎ | utility‎ | functional‎ | function
 
 
 
函式物件
函式呼叫
(C++17)(C++23)
恆等函式物件
(C++20)
透明運算子包裝器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

舊繫結器和介面卡
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(直到 C++20*)
(直到 C++20*)
 
 
explicit operator bool() const noexcept;
(C++11 起)

檢查 *this 是否儲存了一個可呼叫函式目標,即不為空。

目錄

[編輯] 引數

(無)

[編輯] 返回值

如果 *this 儲存了一個可呼叫函式目標,則為 true,否則為 false

[編輯] 示例

#include <functional>
#include <iostream>
 
void sampleFunction()
{
    std::cout << "This is the sample function!\n";
}
 
void checkFunc(std::function<void()> const& 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!

[編輯] 參閱

檢查 std::move_only_function 是否有目標
(std::move_only_function 的公有成員函式) [編輯]