【发布时间】:2020-04-13 11:58:08
【问题描述】:
std::function<void(bool)> f;
std::function<void()> binded_f = std::bind(f, true);
std::cout << (f != nullptr) << " " << (binded_f != nullptr) << "\n";
f(true);
binded_f();
上面的代码给出了输出0 1,并且binded_f()在MSVC中与Unhandled exception at 0x00007FFE7B63A388 in: Microsoft C++ exception: std::bad_function_call at memory location 0x00000096960FA660. occurred崩溃。
貌似调用空函数f没问题,而应用std::bind后会crash。我们应该做什么?我们需要在绑定之前检查一个函数吗?
【问题讨论】:
标签: c++ c++11 lambda std-function stdbind