名稱空間
變體
操作

std::move_only_function::operator bool

來自 cppreference.com
 
 
 
函式物件
函式呼叫
(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*)
(C++17* 前棄用)(C++17* 前棄用)(C++17* 前棄用)(C++17* 前棄用)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(C++17* 前棄用)(C++17* 前棄用)(C++17* 前棄用)(C++17* 前棄用)
(直到 C++20*)
(直到 C++20*)
 
 
explicit operator bool() const noexcept;
(C++23 起)

檢查 *this 是否儲存可呼叫目標,即是否非空。

目錄

[編輯] 引數

(無)

[編輯] 返回值

*this 儲存可呼叫目標,則為 true,否則為 false

[編輯] 示例

#include <functional>
#include <iostream>
 
void sampleFunction()
{
    std::cout << "This is the sample function!\n";
}
 
void checkFunc(std::move_only_function<void() const> 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::move_only_function<void() const> f1{};
    std::move_only_function<void() const> 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_functionnullptr 進行比較
(函式) [編輯]
檢查是否包含目標
std::function<R(Args...)> 的公開成員函式) [編輯]