c++ - 什么是 'valid' std::function?

标签 c++ c++11 std-function

这里:

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/

相关文章:

c++ - 将 shared_ptr<Derived> 作为 shared_ptr<Base> 传递

c++ - 在关闭 Occi::Connection 之前复制 Occi::ResultSet

c++ - 使用智能指针时递归函数中的段错误

c++ - 具有不同签名的 std::function vector

c++ - 将 std::function 与模板一起使用

c++ - 在 C++ 中一次从 stdin 读取一个字节的快速简单方法

c++ - 在 .cpp 中调用 .c 文件

c++ - 将 std::function 作为参数传递时出现问题

c++ - 访问放在类中的对象的方法

c++ - 带有命名空间的模板化结构的 Typedef