简体   繁体   English

导致std :: bad_function_call的原因是什么?

[英]What causes std::bad_function_call?

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. 我见过几个 问题是指std::bad_function_call例外 ,但一直没能谷歌搜索是什么原因导致这个例外,找出任何。

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? 你能给我一些没有其他语义问题的最小例子吗?

Sure- the easiest is where you try to call a std::function that's empty. 当然 - 最简单的是你尝试调用一个空的std::function

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

"Performing a function call without having a target to call throws an exception of type std::bad_function_call" “在没有调用目标的情况下执行函数调用会抛出类型为std :: bad_function_call的异常”

    std::function<void(int,int)> f;
    f(33,66); // throws std::bad_function_call

No credits to me....its Nicolai Josuttis Pundit of C++ Standard Lib 我没有信用....它的C ++标准Lib的Nicolai Josuttis Pundit

in my case was the problem was in capture list. 在我的情况下,问题是在捕获列表中。 i have a recursive lambda function. 我有一个递归的lambda函数。

//decl
std::function<void(const SBone*, const core::vector3df&, const core::quaternion&)> f_build;
f_build = [&f_build](const SBone* bone, const core::vector3df& pos, const core::quaternion& rot)
{
...
}

missing & from f_build in capture list generate a bad call. 从捕获列表中的f_build中丢失并生成错误的调用。

Call of a temporary function also can throw: 调用临时函数也可以抛出:

struct F
{
    const std::function<void()>& myF;

    F(const std::function<void()>& f) : myF(f) {}

    void call()
    {
        myF();
    }
};

int main()
{
    F f([]{ std::cout << "Hello World!" << std::endl;});

    f.call();

    return 0;
}

But this depend on compiler (vc++ throws, g++ not). 但这取决于编译器(vc ++ throws,g ++ not)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 stst :: bad_function_call的callstack - Callstack for std::bad_function_call 意外的std :: bad_function_call递归 - unexpected std::bad_function_call in recursion 从外部函数构造 std::function 给出 std::bad_function_call - Constructing std::function from extern functions gives std::bad_function_call c++ std::map 比较函数导致运行时“bad_function_call” - c++ std::map compare function leads to runtime "bad_function_call" std :: bad_function_call在visual studio中调用std :: swap时 - std::bad_function_call when calling std::swap in visual studio 从不同的 cpp 调用回调函数会导致 bad_function_call - Calling Callback function from different cpp causes bad_function_call std :: bad_function_call自调用递归lambda但没有提示 - std::bad_function_call of self-calling recursive lambda but no hints 为什么我的线程池有时会抛出 `std::bad_function_call` 或 `double free or corruption (!prev)` - Why does my thread pool sometimes throw `std::bad_function_call` or `double free or corruption (!prev)` 将 avx 变量传递给 std::function 时引发 bad_function_call 和分段错误 - bad_function_call thrown and segmentation fault caused when passing avx variables to std::function 运行 dpcpp 代码时终止代码为 std::bad_function_call - Terminating code as std::bad_function_call when running the dpcpp code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM