什么原因std :: bad_function_call?

问题描述:

我看过几个 问题,指的是 std :: bad_function_call exception ,但是还没有找到任何由谷歌关于什么导致这个例外。

I've seen a few questions that refer to the std::bad_function_call exception, but haven't been able to find out any by Googling about what causes this exception.

什么样的行为应该导致这个异常?你能给我一些没有其他语义问题的例子吗?

What kind of behavior is supposed to cause this exception? Can you give me minimal examples that don't have other semantic problems also going on?

确定 - 最简单的是你在哪里尝试调用一个 std :: function 这是空的。

Sure- the easiest is where you try to call a std::function that's empty.

int main() {
    std::function<int()> intfunc;
    int x = intfunc(); // BAD
}