这里:
http://en.cppreference.com/w/cpp/utility/functional/function
operator bool
描述为:“检查存储的可调用对象是否有效”。
大概默认构造的 std::function
是无效的,但这是唯一的情况吗?
另外,它如何检查它是否有效?
operator()
引发 std::bad_function_call
的情况是否正是对象无效的情况?
最佳答案
它写得不好,你的困惑是有道理的。 “有效”的意思是“有目标”。
std::function
在被分配了一个函数时“有一个目标”:
std::function<void()> x; // no target
std::function<void()> y = some_void_function; // has target
x = some_other_void_function; // has target
y = nullptr; // no target
x = y; // no target
他们应该在使用之前定义“有效”,或者只是坚持使用官方措辞。
关于c++ - 什么是 'valid' std::function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11887896/