std::move_only_function::operator bool
來自 cppreference.com
< cpp | utility | functional | move only function
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!
[編輯] 參閱
(C++23) |
將 std::move_only_function 與 nullptr 進行比較(函式) |
檢查是否包含目標 ( std::function<R(Args...)> 的公開成員函式) |