std::move_only_function::operator bool
来自cppreference.com
< cpp | utility | functional | move only function
explicit operator bool()constnoexcept; | (C++23 起) | |
检查 *this 是否存储可调用目标,即非空。
目录 |
[编辑]参数
(无)
[编辑]返回值
若 *this 存储可调用目标则为 true,否则为 false。
[编辑]示例
运行此代码
#include <functional>#include <iostream> void sampleFunction(){std::cout<<"这是样例函数!\n";} void checkFunc(std::move_only_function<void()const>const& func){// 用 operator bool 确定可调用目标是否可用。if(func){std::cout<<"函数非空!调用函数。\n"; func();}elsestd::cout<<"函数为空。不做任何事。\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: 函数为空。不做任何事。 f2: 函数非空!调用函数。 这是样例函数!
[编辑]参阅
(C++23) | 比较 std::move_only_function 与 nullptr(函数) |
检查是否包含目标 ( std::function<R(Args...)> 的公开成员函数) |