std::function<R(Args...)>::operator bool
來自 cppreference.com
< cpp | utility | functional | function
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 的公有成員函式) |